Random numbers in PHP

How to generate random numbers in PHP. Generation using the rand() and mt_rand() functions available in PHP.

The generation of random numbers is something basic and very common in any application. Of course PHP is no exception. In this article we are going to address the generation of random numbers with various mechanisms and examples. It is important since PHP has a series of functions to generate random numbers with various features. We are going to see them explaining their uses and differences in operation.

Using the rand() function for random numbers

The most basic way to generate a random number in PHP consists of two steps:

// feed the random generator srand (time()); //generate a random number $random_number = rand(1,100);

As we can see, in the first step the srand() function is used to feed the random number generation seed.

The step of calling srand() is only necessary in versions prior to PHP 4.2.0, since from this version this step is done automatically.

The srand() function needs to be sent a value to feed the seed. We send what time() returns, which is a timestamp with the number of seconds since the start of 1970.

We then generate a random number with the rand() function which optionally receives a pair of values, which are the minimum and maximum of the generated random numbers. In the previous case, a random number between 1 and 100 is obtained, including these two values ​​among the possible ones.

If rand() is not told anything, the minimum value will be zero. The maximum value depends on the platform where PHP is running, for example on Windows the maximum value would be 32786 (this may also depend on the PHP version). If we want to make sure that this maximum value is greater, then it is convenient to define the maximum and minimum values ​​when calling the function.

See also  What does it mean that Javascript is asynchronous?

Advanced random number generation with mt_rand()

PHP has other functions to generate random numbers, apart from the ones we have seen, which use improved algorithms to get random numbers.

The PHP function mt_rand() generates randoms with an algorithm that is on average 4 times faster than the algorithm used by rand().

Using mt_rand() is similar:

//we feed the random generator mt_srand (time()); //generate a random number $random_number = mt_rand(0,5);

You must first use mt_srand() to start generating random numbers with a seed.

Again, the step of using mt_srand() as of PHP 4.2.0 is not necessary, because it is done automatically.

Then the random numbers are generated with mt_rand(), to which we pass the range of values ​​we want to obtain, with the minimum and maximum parameters. In our example we will get random values ​​between 0 and 5.

Conclusion on random numbers in PHP

As you have seen, the Random number generation in PHP is very simplesince we simply need to call the functions that the language itself provides us.

Now it is about making the necessary transformations to adapt the numbers generated to our needs. For example, you might need random numbers to have decimals. In this case we can try the FAQ trick:

Loading Facebook Comments ...
Loading Disqus Comments ...