Random Alphanumeric String Generator Script in PHP

These are two functions that you can use to assign random strings of any length. You can use these for session IDs, tracking IDs, validation checks, password generators, etc.
Demonstration of randomly-generated strings, 16 characters long; refresh the page to change the values:

The first function: assign alphanumeric value

The first function is long but simple. We put in a number, 1 to 36, and assign an alphanumeric value to it.

The second function: generate random string

The second function is what makes this work. We feed it the length of the string we want, and it gives us a random string of alphanumeric characters that length.
Demonstration (reload to change values)
Here's the code of the random string generator function:
Here are the steps of the code, explained:
  1. Check that the length is not 0.
  2. Start a loop. We loop once for each character we need for the string.
  3. We "seed" the random number function with the current microtime (times 1000000).
  4. We choose a random number between 1 and 36 (because our first function, assign_rand_value($num) accepts that range of numbers.
  5. We then use assign_rand_value($num) to change the random number into its corresponding alphanumeric number.
s

Altering the random string generator

For strings of only numbers, keep the first function and add this function (the changes have been put in bold)
For only letters, keep the first function and add this function (the changes have been put in bold)

»» more PHP scripts at www.i-fubar.com