Generating random numbers in php

In this php tutorial we will learn how to generate random numbers in php. To generate random numbers in php is a quite easy, just you have to call the built-in php function rand() and that’s it. Rand() will return you the random number, let’s have a look over the example given below.

<?php
$Random_number=rand();

echo $Random_number;

?>

When you run this php script, every time you will get a random number but what about if you want to get random number within a range???? To get a random number within a range is also a quite easy method just you need to pass the two arguments to the rand() function. First argument will be the starting number of the range and second argument will be the ending number of the range, now every time you will get a random number that will lie in this range, let’s have a look over example to clearly understand what I try to explain

$Random_number =rand(1,5);


echo $Random_number;

Now some time you will get 1, some time 2 and some time 3, etc. It will not give you any number less than 1 or greater than 5

So this is the way to generate random numbers in php.



1 comments:

  • Anonymous
     

    Thats what i wanted.

    Thanks!