Thursday, January 10, 2013

Generate random numbers in php

How to generate random numbers using PHP?

The following example explains you how to generate random numbers in php
Random Numbers are very common in our day today life. For an example think of a dice game or a lottery game. When drawing a lottery, there is a lottery machine that use to draw the winning numbers. It has the feature of random selection of lotto balls. It is difficult to predict what are the winning numbers till the end. Because it produce a random combination of numbers. But we can guess the the selection will be between 1 and 99 (for example. some times there are set of duplicate set of lotto balls for a draw.). Likewise we can generate random numbers between given two numbers using PHP.

PHP Random number generator

Random number generation methods

  1. Physical Methods
  2. Computational methods

Physical methods are earliest methods that used to generate random numbers. Coin flipping, dice throwing are examples for physical methods. Computational methods use mathematical algorithms to populate random numbers. Physical methods are very slow.

You can use rand(), or mt_rand() functions to generate random numbers in php

rand() function returns an integer value as a random number. Also you can specify lower and upper limit for generated number. also you can get the largest random number can be generated using rand() function. rand() function is 4 times slower than mt_rand(). So it is better to use mt_rand() method to make random numbers in php. Also mt_rand() produce bigger random value comparing to the rand() method. Usually it will be equal to maximum size of the integer value that can be produce by the system. maximum integer value is platform dependent.

Usage of random numbers

  1. Prepare online MCQ papers. (use to provide a random set of questions from a set of questions).
  2. Games (ex. dice, coin).
  3. Show selected posts (e.g. display Random posts in your website).
  4. Shuffle a play list.
  5. Generate CAPTCHA verification images.
  6. Create random passwords.
  7. Prevent caching web pages. (e.g. When using ajax calls append a random number at the end of the URL)

1. How to generate random numbers using php?

echo rand();//traditional pseudo-random number

echo '<br/>';

echo mt_rand();//Generate better random number. This is the newest method

You can use above sample code to generate random integers in php. rand() function produce a small value than mt_rand(). All generated random numbers in the range of 0 to max random number that can be generated by the system.

2. How to determine maximum random values that can be generated using php?

echo getrandmax(); /* Maximum random value that can be generate using rand() method
This will out put 32767 in windows 7 - 32 bit computer */

echo '<br/>';

echo mt_getrandmax(); /* Maximum random value that can be generated using mt_rand() method This will out put 2147483647 in windows 7 - 32 bit machine */

Maximum random number that can be generated by the system is platform dependent. It returns the largest possible integer value that can be return by rand() or mt_rand() function. Usually this is equal to the maximum integer value of your system. This also platform dependant. You can find the maximum integer value as follows.

echo PHP_INT_MAX;//maximum possible integer value

3. How to generate a random number within a specific range in php?

$min = 10; // The lowest value of the random number to generate
$max = 100; // The maximum value to generate

echo rand($min,$max); /* Generate random number between 10 and 100*/

echo '<br/>';

echo mt_rand($min,$max); /* Get random value between 10 and 100 */

Suppose that you need to generate random numbers between specific range, you can do it by passing minimum and maximum values to the rand() or mt_rand() functions. Then the generated random integers will be in the range that you have provide. By default minimum random number is 0 and the maximum random number is getrandmax() or mt_getrandmax() value. Maximum random numbers are platform dependent.

4. PHP random number examples

Think that you have to generate 10 random numbers between 1 and 100. Also you have to count the number of times each number generated. Also you need to print the summary of each occurrence Following example describes you how to do it.

$random_numbers =array();
$rnd = mt_rand(1,100);

foreach($random_numbers as $number=>$times){
echo 'Number '.$number.' was generated '.$times.' times(s)<br/>';

Random numbers are very useful. As a example you have a play list with 20 songs. What you have to do is play randomly selected songs of re arrange/shuffle the play list. You can use random numbers to perform the task.

Following example shows you how to select a day of the week randomly using php.

echo $days[mt_rand(0,count($days)-1)];

Bellow example describes you how to generate random numbers 1 to 100

echo mt_rand(1,100);


Marshal Sampson said...

I want you to help me out with a small script requiring the idea of random number generation. Database will be involved. I want to create a 2 step authentication script that uses email.

First, the script should receive visitor's email address on a page carrying email as the only field.

Secondly, the script generates a 5 digits random number and save to a database against the visitor's email, and also sends a mail carrying the 5 digits code to the email address and also display a message requesting the visitor to login with the code received in his email inbox.

Next, the visitor is returns to login to proceed to a secured page on success or return back to attempt login with the code.

What will it take you to give me this help? I actually wanted to create the same thing for short code sent as sms to mobile phone instead of email but couldn't find a company that could assist so i decided to use this. Reach me through

Anu said...

Really nice information you had posted. Its very informative and definitely it will be useful for many people
iOS Training in Chennai
Android Training in Chennai
php Training in Chennai