Posts Tagged ‘mt_rand()’

Generating Random Numbers Within a Range in PHP by Mrinmoy

Problem-You want to generate a random number within a range of numbers.
Solution-Use mt_rand();
The proper syntax is > $random_number = mt_rand($lower,$upper);
Discussion:
Generating random numbers is useful when you want to display a random image on a page, randomize the starting point of a game, select a random record from a database , or generate a unique session identifier.

Calling mt_rand() without any arguments it returns any random numbr between 0 to maximum random number which we can easily get using mt_getrandmax().
Generating truly random number is very difficult for any computer. Computers excel at following instructions methodically; they’re not so good at spontaneity. If you want to instruct a computer to return random numbers, you need to give it a specific set of commands; the very fact that they are repeatable undermines the desired randomness.
PHP has two different random number generator one is rand() and another is mt_rand().
In my view mt_rand() is better. mt_rand () is more faster and accurate random number generator.

Okay I think you already take this lesson completely..IF you are not please inform me..I will help you to understand it.
Otherwise you can check these following link for more details:
Details about mt_rand() on php.net
mt_rand() on w3schools

Example:
<?php
$random_number=mt_rand(1,100);
echo $random_number;
echo "mt_rand(100,200)";
?>

The output will be like this:
12 //——>Random Number between 1 to 100
145 //——>Random Number between 100 to 200

We will cover next session soon..stay connected…–>iMrinmoy