Home » PHP5 March 2008

PHP: Generate random number and string

Here are code samples to generate random number and strings in PHP.

Generate random numbers between 1 and 1000

Using mtrand() function for better random number generation

Generate a unique combination of numbers and letters. Works with PHP5 and higher.

Using md5() and uniqid() function to make the string more complex. md5() function generates 32 character long string. uniqid() works only with PHP5 or higher.

Generate random string from a given string. Randomly shuffling a given string.

Generate random number using time() function. The number changes every second. This returns the current unix timestamp.

Hope it helps.
Thanks.

PHP

Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed
  • you might know that no random no is 100% pure random no as it depends upon particular factor for generating the random values. It’s better to seed the random no generator before generating the random number. I better prefer the approach given in the following link.
    http://www.php.net/srand

  • Thanks. But I was not illustrating the best random no. generation method. I was just showing different approaches to do this thing.