» Birthday Bundle - Over $400 worth of Envato files for just $20
  Posts tagged 'random'

Display Values from an Array in Random Order

Posted on October 2, 2008, Filled under PHP,  Bookmark it

This is a function that randomizes the order of the elements in an array.

<?php
function randomize_array($array)
{
$rand_items = array_rand($array, count($array));

$new_array = array();

foreach($rand_items as $value)
	{
	$new_array[$value] = $array[$value];
	}

	return $new_array;
}

$array = array('sleep' => 'Delay execution',
                    'show_source' => 'alias for highlight_file()',
                    'str_ireplace' => 'PHP5 Function',
                    'sizeof' => 'Alias for count',
                    'parse_url' => 'Parse a URL and return its components');

echo "<pre>"; print_r($array); echo "</pre>";

echo "<pre>"; print_r(randomize_array($array)); echo "</pre>";

/*
-- Initial Array

Array
(
    [sleep] => Delay execution
    [show_source] => alias for highlight_file()
    [str_ireplace] => PHP5 Function
    [sizeof] => Alias for count
    [parse_url] => Parse a URL and return its components
)

-- An example of a random result

Array
(
    [sizeof] => Alias for count
    [parse_url] => Parse a URL and return its components
    [sleep] => Delay execution
    [str_ireplace] => PHP5 Function
    [show_source] => alias for highlight_file()
)
*/
?>

NOTE: A similar function in PHP is shuffle() which assigns new keys to elements in the array. It removes any existing keys that may have been assigned, instead of just reordering them as our randomize_array() function does.

Show random image(s) from a directory

Posted on September 21, 2008, Filled under PHP,  Bookmark it

Hi,

If you need to show images randomly from a directory then this script can help you.

index.php

Let’s configure some variables: $extensions, $images_folder_path & $url_to_images_folder.

<?php
// extensions to be checked

$extensions = array('jpg','jpeg','gif','png','bmp');

// images folder

$images_folder_path = '/home/site/public_html/images/';

// url to folder

$url_to_folder = 'http://www.yoursitename.com/images/';

Read more from this entry…

Hi,

Here’s a script which randomly selects a value from an array:

<?php
function selectRandomValueFromArray($array, $range = '')
{
srand((float) microtime() * 10000000);

if(empty($range))
{
$last_key = count($array) - 1;
$range = array(0, $last_key);
}

$rand_array = rand($range[0], $range[1]);

$result = $array[$rand_array];

return $result;
}

$array = array('apple', 'pine', 'pear', 'strawberry', 'cherries', 'mango', 'grape', 'watermelon', 'papaya');

/*
1st argument: array
2nd argument (optional): the array range (2 values) of the keys
*/

echo selectRandomValueFromArray($array); // select random value from all

echo "<br />";

echo selectRandomValueFromArray($array, array(0, 3)); // select random value from  the first 4 values of the array

echo "<br />";

echo selectRandomValueFromArray($array, array(2, 5)); // select random value between the 3rd and the 6th value of the array
?>

NOTE The array starts from 0. If you need, for instance, to set a range from the 2nd to the 5th value of the array then you should use the following argument: array(1, 4).

The advantage of this function, compared to array_rand() is that it can randomly select values from a specified range.

Good luck!

Creating a Random Quote Script

Posted on August 29, 2008, Filled under PHP,  Bookmark it

This is a Random Quote Script.

<?php
// Credits: http://www.bitrepository.com/

srand((float) microtime() * 10000000); // IF PHP Version < 4.2.0

// Create the array first
$quotes = array();

// Fill it with quotes

$quotes[] = array('quote'  => 'Time is money.',
                  'author' => 'Benjamin Franklin');

$quotes[] = array('quote'  => "And in the end, it's not the years
in your life that count.
 It's the life in your years.",
                  'author' => 'Abraham Lincoln');

$quotes[] = array('quote'  => "The secret to creativity is knowing
how to hide your sources.",
                  'author' => 'Albert Einstein');

$quotes[] = array('quote'  => "A mind that is stretched by a new experience
 can never go back to its old dimensions.",
                  'author' => 'Oliver Wendell Holmes');

/*
NOTE: To add more quotes just continue filling the array like this:

$quotes[] = array('quote'  => "THE QUOTE HERE",
                  'author' => "QUOTES'S AUTHOR");

*/

$rand_key = array_rand($quotes, 1);

$quote_array = $quotes[$rand_key];

$quote = $quote_array['quote'];
$author = $quote_array['author'];

echo $quote.' - '.$author;
?>

How to generate a random password in PHP

Posted on August 29, 2008, Filled under PHP,  Bookmark it

This function generates random passwords. It’s useful when you have to reset someone’s account or when you create accounts that need a default password.

<?php
/*
Credits: Bit Repository

http://www.bitrepository.com/web-programming/php/generating-a-random-password.html

*/
function generate_random_password($chars = 7, $type = '')
{
$letters_array = range('a','z');
$numbers_array = range(1,9);

srand((float) microtime() * 10000000); // if PHP Version < 4.2.0 

if($type == 'letters')
{
$array = $letters_array;
}
else if($type == 'numbers')
{
$array = $numbers_array;
}
else
{
$array = array_merge($letters_array, $numbers_array);
}

$rand_keys = array_rand($array, $chars);

$password = '';

foreach($rand_keys as $key)
	{
	$password .= $array[$key];
	}

return $password;
}

$password = generate_random_password();

// Outputs a 7 characters password (default)
echo $password;

$password = generate_random_password(10);

// Outputs a 10 characters password
echo $password;
?>


If you wish to generate a 10 characters password containing only numbers then here's how you can do it:
$new_password = generate_random_password(10,'numbers');

// Outputs a numeric password containing only numbers.
echo $new_password;

NOTE: You can add ‘letters’ for the second parameter if you want to have only letters in the password.

Read the rest of this entry…