Random integer generator

rand ( min,max);
min ,max : (Optional) Minimum and maximu value ( both inclusive ) of random integer to be generated ( both to be given )

We can generate random value by using rand() PHP. We can specify a range and the random value between those two ranges (inclusive) will be generated.
echo rand(); // Random Output 16513
echo rand(8,10); // Random Output starting from 8 to 10 
This output is not cryptographically secure values , so for cryptographically secure value, consider using random_int(), random_bytes
Note that here we are generating a random number only. To get any random characters we can use arrays. The details on how to generate random characters is available here.

Random number of different length

We can create a script to generate random number of different length. Let us try to develop a script to print random number of 5 character length.

Here it is
<?Php
$no_of_digits=5;
$var='';
for($i=1; $i<=$no_of_digits; $i++){
$var .=rand(0,9);
}
echo "<br>Random Value = $var"; 
?>
Output is here
Random Value = 33369
Read here how to generate random string having alphabets and digits
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com




    princy

    02-02-2012

    how to generate the random number in php

    Post your comments , suggestion , error , requirements etc here .




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2023 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer