Categories
PHP

Generating a random number

PHP provides the rand( ) and mt_rand() functions, which returns values from a generated sequence of pseudo-random numbers. The srand( ) and mt_srand() functions seed the algorithm and need to be called before the first use of the rand( ) or mt_rand() functions in a script to return the same random numbers each time a script is called.

  1. rand() and mt_rand()
  2. getrandmax() and mt_​getrandmax()
  3. srand() and mt_srand()

rand()

<?php
 //Syntax 1:
 rand(): int

 //Syntax 2:
 rand(int $min, int $max): int

Since PHP version 7.1 the rand() function is an alias of mt_rand() function, it optionally takes two parameters:

  1. $min (default value is 0): The lowest value to return
  2. $max (default value is getrandmax() ): The highest value to return

When called with no arguments, rand() returns a random number between 0 and the value returned by getrandmax(). When rand() is called with two arguments, the $min and $max values, the returned number is a random number between $min and $max. See this example:

<?php
 echo rand();
 #output something similar to: 1463752631

 echo rand(1, 10);
 #output something similar to: 7

mt_rand()

The rand() and mt_rand() functions shares the same syntax and since PHP version 7.1 there is no difference between both functions as described in the PHP documentation: As of PHP 7.1.0, rand() uses the same random number generator as mt_rand().

Before PHP version 7.1, if you need a very large amount of random numbers use the mt_rand() function it has a period of 219937-1, far better than rand(): 232.

getrandmax()

<?php
 //Syntax
 getrandmax(): int

The getrandmax() function returns the largest possible random value, see this example:

<?php
 echo getrandmax();
 # 2147483647

mt_getrandmax()

The getrandmax() and mt_getrandmax() functions share the similar syntax, and since the PHP version 7.1 both returns the same output.

srand()

<?php
 //Syntax
 srand(int $seed = 0, int $mode = MT_RAND_MT19937): void

The srand() function takes two parameters:

  1. $seed: An arbitrary integer value.
  2. $mode: Specify an algorithm to use with the following constants:
    • MT_RAND_MT19937
      Uses the fixed, correct, Mersenne Twister implementation, available as of PHP 7.1.0.
    • MT_RAND_PHP
      Uses an incorrect Mersenne Twister implementation which was used as the default up till PHP 7.1.0.

By default PHP automatically seeds the random number generator functions. You can manually seed the random number generator to generate a predictable series of values, see the following example:

<?php
 srand(545);
 echo rand(1,7) . ' ' .
      rand(1,7) . ' ' .
      rand(1,7) . ' ' .
      rand(1,7);
	  
 # Prints: 5 3 6 5
 # Because a specific value was passed to srand(),
 # the same random number will genrated each time,
 # you run the code.

Note: If you want to generate a unique random number each time the script runs, don’t use srand() or mt_srand() function to seed the rand() or mt_rand() functions as it is done automatically.

mt_srand()

The mt_srand() and srand() functions share the same syntax and since PHP version 7.1 the srand() has been made an alias of mt_srand().


Doing Math:

  1. Finding absolute value, lowest value, and highest value
  2. Using Ceil, Floor, and Round functions
  3. Generating random numbers
  4. Converting numbers between decimal, binary, octal, and hexadecimal
  5. Trigonometry, Exponential and Logarithmic Functions