Wednesday, June 13, 2007

Generate a Random Letter in ASP

Generating a random number in ASP is easy; generating a random letter is just as easy.

Simply use the random number generater to generate a number between 26 and 97. This number cooresponds to the character (CHR()) of the letters a - z.

If you want to use other characters, here's a chart (use the decimal column).

Here's the function to generate a number between 2 numbers:
Function RandomNumber(LowNumber, HighNumber)
RANDOMIZE
RandomNumber = Round((HighNumber - LowNumber + 1) * Rnd + LowNumber)
End Function
Assign the function to a variable and pass in the LowNumber (26) and the HighNumber (97) and convert the value returned to the character it represents:
RandomLetter = CHR(RandomNumber(97,122))
That's it!

3 comments:

Anonymous said...

In the function. Do not use Round(...). Use Int(...) instead or you will find your characters returning 97-123 (adding the '{' character).

Anonymous said...

thank you, it helps me

Anonymous said...

Your function returns not only letters: numbers from 91 to 96 are special characters....