Back to DFS's PHP Page


Generating Random Values

One of the major uses for computers is to simulate what happens in the real world. It is up to the programmer to write the programs that mimic what is happening around us. Fundamental to this is the ability to generate random values and take action based on these values.

In many programming languages, generating random values is a two-step process.

  1. You need to "seed" the random number generator. In Pascal this is done using the procedure randomize; in C it is the function srand(). This process establishes a kind of table from which values can be gotten.
  2. To obtain a "random" value, you call a function which accesses the table generated by the seed routine. In Pascal, the function is random; in C, it is rand.

Failure to seed the generator normally resulted in the same sequence of values each time the program was run. While this was useful for debugging, it was not good for a simulation.

Reseeding the generator during the execution of a program or script was also undesirable.

Before PHP 4.2.0, the PHP programmer was responsible for seeding the generator. This was typically done in a manner such as the following:

// seed with microseconds
function make_seed() {
    list($usec, $sec) = explode(' ', microtime());
    return (float) $sec + ((float) $usec * 100000);
}
srand(make_seed());

Starting with PHP 4.2.0, the seeding is automatically done by PHP.

To get a "random" integer value, use the following syntax:

int rand ( [int min, int max] )

If the range limits are not given, the value returned will be between 0 and RAND_MAX, inclusive.

If the limits are specified, e.g.

$die = rand(1, 6);
the value returned will one of the set {1, 2, 3, 4, 5, 6}.

Generating Non-Sequential Values

To generate one of the odd Natural numbers less than 10 (i.e., one of the set {1, 3, 5, 7, 9}), we would need to generate one of five values and then proceed to select a corresponding member of the set. This can be done by setting up a table of the correspondences and determining a mathematical relationship.

rand(0, 4)$ran
01
13
25
37
49

If the columns are treated as the traditional x and y, the relationship could be expressed as

y = 2x + 1
resulting, by simple substitution:
$ran = 2 * rand(0, 5) + 1;

Generating Alphabetic Characters

For some situations, you may want to generate one of the letters of the alphabet. Since rand() returns integer values, you would need to convert the value into data of type Char.

$ranletter = chr( ord("a") + rand(0, 25) );
The above statement uses the value returned by rand() as an offset into the set of values used to represent the lowercase letters in the ASCII character set. First it gets the ordinal value of the character "a" (which is 97) from the function ord() and then it adds the value returned by rand() (which is in the range 0 to 25). The resultant integer value is then passed to the function chr() which returns the corresponding character, which is finally stored in the variable $ranletter. N.B. This will not work if the system is using EBCDIC, but this is rarely an issue anymore.

Or you may want to generate just one of the lowercase vowels. This can be accomplished in a number of ways, one of which follows.

   switch ($i = rand(0, 4) ){
     case 0 :
        $ranvowel = "a";
        break;
     case 1 :
        $ranvowel = "e";
        break;
     case 2 :
        $ranvowel = "i";
        break;
     case 3 :
        $ranvowel = "o";
        break;
     case 4 :
        $ranvowel = "u";
        break;
   }

© DFStermole: 2002
Created: 21 Oct 02
Last Modified: 21 Oct 02