This page implements a random number (and string) generator. The form below provides a way to test the functions. The PHP source code for the RandNumber and RandString functions is available from the links. To avoid multiple POSTs and page reloading, the value generation is done using a JavaScript AJAX call. This is done using the Prototype JavaScript framework.
PHP Random
PHP has two different functions for generating random number, rand() and
mt_rand(), which is more random and faster than rand(). This example uses the
mt_rand() function.
Random numbers – source code
The example generates a number in the range of 0 to maximum with maximum being the value returned by the mt_getrandmax() function.
Random strings – source code
The example generates a string of random numbers, uppercase and lowercase letters and other characters depending on the character types included. This is done by getting a random number in seven ASCII value ranges, three ranges for the “0-9″, “A-Z”, and “a-z” and then four more ranges for the other characters. These are then combined and added to the result until the requested number of characters has been obtained.