Site Navigation:

Home // Tutorials // PHP // Generating Random Numbers

Generating Random Numbers

1
Generating a random number seems useless, but once you get into working with arrays, it's very useful to have. One thing you can use it for is random links, or possibly random quotes.

-- NOTE: This next section only applies to PHP versions lower than 4.2.0. If you have 4.2.0 or higher, skip to Number 2 --

When you generate a random number, you must ALWAYS seed the rand() function with srand() first. Here's how:

<?php
// Seed random function
srand((double)microtime()*1000000);
?>


That code right there CAN'T be changed no matter what. Changing anything in that line will cause the random function to not work.

* Also note that you only need to call srand() once per script. If you have multiple random functions, you only need to use that code once. Basically what it does is get you the maximum randomness. Trying to call it again isn't possible, because the randomness is already maxed out.
2
The next step in generating a random number is pretty straight-forward. The function you'll need to use is rand(). Here's how:

<?php
// (< 4.2)Seed random function
srand((double)microtime()*1000000);

// "Randomize" - rand([int min, int max])
$randomNo = rand(0,10);
?>


The code added on simply does this: fetches a random number between 0 and 10. The first argument (0 in this case) tells the lowest number that can be retrieved, and the second argument (10 in this case) tells the highest number that can be retrieved.

* (PHP lower than 4.2) Note that if you don't seed the rand() function (see section 1), the rand() function won't work.

The only thing left to do now is echo (print out) the random number:

<?php
// Seed random function
srand((double)microtime()*1000000);

// "Randomize" - rand([int min, int max])
$randomNo = rand(0,10);

// Display random number
echo $randomNo;
?>


And that's the final script. Read section 3 to find out how to apply this to an array.
3
The random number generator can be used to get a random entry in an array. The first thing you need to know if you want to do this is how to make an array and how to call entries from it. Here's how:

<?php
// Create the array
$fruits = array("apple", "banana", "peach", "plum");

// Call the 3rd entry (peach) from the array
echo $fruits[2];
?>


Remember that all arrays start with 0 rather than 1. So, the first entry (in this case "apple") would be called with $fruits[0].

Now that you've seen this, you might see where the random number fits in. Incase you don't see that, it's basically this:

You generate the random number, and set that random number to a variable, let's say, $randomNo. Then, instead of putting in a number between the brackets ([]) in the array variable (such as, $fruits[2]), you'd put the variable that the random number is assigned to, which in the end would look like: $fruits[$randomNo].

The last thing you'd need to do is instead of putting 10 as the highest number that the rand() function can pick, you replace it with a function that will count how many entries there are in the array. That function is count(). So, if you wanted to count how many entries were in the fruit array, it would look something like:

<?php
// Create the array
$fruits = array("apple", "banana", "peach", "plum");

// Count how many entries in this array
$arrayCount = count($fruits);
echo $arrayCount;
?>


But, since we're starting with 0 in the array rather than one, we'll need to subtract 1 from the total entries in the array. So, instead of 4, we'll get 3, which is the highest number that the rand() function can pick. And then you simply replace the maximum number in the rand() function with the variable assigned to the number of entries in the array, minus 1.

So, to put it all together and get the random fruit:

<?php
// Create the array
$fruits = array("apple", "banana", "peach", "plum");

// Count how many entries in this array & subtract 1
$arrayCount = count($fruits) -1;

// Seed random function
srand((double)microtime()*1000000);

// "Randomize" - rand([int min, int max])
$randomNo = rand(0,$arrayCount);

// Grab a random entry from the Fruit array
$randFruit = $fruits[$randomNo];

// Display the random fruit
echo $randFruit;
?>


Make sense? It should, if not, go check out the functions I mentioned at www.php.net and try this tutorial again. If you still have trouble, go over to www.neverside.com and ask.

Good luck! -Bryan
spacer

This Tutorial was by Bryan, Please post any questions in the forum. Thank you.
BACK TO TUTORIALS



Home Tutorials Free Stuff About Gallery Forum Links Contact

Copyright © Robouk 2001.

Dreamweaver®, Flash® and Photoshop® are either registered trademarks or trademarks of Adobe Systems Incorporated in the United States and/or other countries.
Hosted by Pixelcode - web design tutorials.