Random Numbers PHP

The following functions help you generate pseudorandom numbers. There are wrappers for the randomizing functions offered by your operating system, and there are functions based on the Mersenne Twister algorithm. The Mersenne Twister functions are faster and return numbers with a much better distribution suitable for cryptographic applications. The algorithm was developed by Makoto Matsumoto and Takuji Nishimura. Pedro Melo refactored an implementation by Shawn Cokus in order to add support to PHP.
integer getrandmax()
The getrandmax function returns the maximum random number that may be returned by the rand function.

<?
print(getrandmax());
?>

integer mt_getrandmax()
The mt_getrandmax function returns the maximum random number that may be returned by the mt_rand function.

<?
print(mt_getrandmax());
?>

double lcg_value()
The lcg_value function returns a number between 0 and 1 using an algorithm called a linear congruential generator, or LCG. This is a common method for generating pseudorandom numbers. The generator is seeded with the process identifier.

integer mt_rand(integer min, integer max)
The mt_rand function uses the Mersenne Twister algorithm to return a number between the two optional arguments, inclusive. If left out, zero and the integer returned by themt_getrandmax function will be used. Use mt_srand to seed the Mersenne Twister random number generator.

<?
//seed the generator
mt_srand(time());
//get ten random numbers from 1 to 100
for($index = 0; $index < 10; $index++)
{
print(mt_rand(1, 100) . "<BR> ");
}
?>

mt_srand()
The mt_srand function seeds the Mersenne Twister random number generator. It is best to call this function once before using the mt_rand function.

integer rand(integer lowest, integer highest)
The rand function returns a number between the two optional arguments, inclusive. If left out, zero and the integer returned by the getrandmax function will be used. Use the srand function to seed the random number generator.

<?
srand(time());
//get ten random numbers from -100 to 100
for($index = 0; $index < 10; $index++)
{
print(rand(-100, 100) . "<BR> ");
}
?>

srand(integer seed)
The srand function seeds the random number generator. It is best to call this function once before using the rand function.

string tempnam(string directory, string prefix)
The tempnam function returns the complete path to a unique temporary filename. This guarantees you will not overwrite an existing file. You must take the responsibility to create and then destroy the file.

The directory argument specifies a directory to put the file in, but it will be discarded if a default temporary directory is defined in an environment variable. Under UNIX this variable is called TMPDIR. Under Windows it is called TMP.You must also specify a prefix for the file, but you may pass an empty string. It's a good idea to pass a meaningful prefix, which will allow you to distinguish between temporary files created by different processes. Note that no suffix is added to the file. Under Windows you may want to add .tmp or some other file extension. An empty string is returned on failure. This function is similar to uniqid.

<?
$myFile = tempnam("C: emp", "data");
if(strlen($myFile) > 0)
{
print($myFile);
}
else
{
print("Couldn't make temporary name");
}
?>

string uniqid(string prefix, boolean use_lcg)
The uniqid function joins the prefix argument to a random series of numbers and letters, which are generated based on the system clock. The prefix may be up to 114 characters long and the unique string is always 13 characters long.If the optional use_lcg argument is TRUE, nine additional characters will be added to the end of the return string These characters are generated by the same algorithm used by the lcg_value function, so they will be a period followed by eight digits. Because the lcg_value function seeds itself with the process ID, turning on this flag may not actually add much randomness.

Compare this function to tempnam.

<?
print(uniqid("data"));
?>

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

PHP Topics