Tuesday, August 29, 2006

Generating a Random Number in ASP

Here's a function to generate a Random Number. Simply feed it the top end value, so to generate a random number from 1 - 10, you call it with:


Function RandomNumber(intHighestNumber)
RandomNumber = Round(Rnd * intHighestNumber) + 1
End Function

What is you want to generate a random number between two other numbers, like between 20 and 40?


Function RandomNumber(LowNumber, HighNumber)
RandomNumber = Round((HighNumber - LowNumber + 1) * Rnd + LowNumber)
End Function


Anonymous said...

The second function does not need the "+1" in it. if you use the example you will get numbers from 20-41 inclusive.

Huck said...

This is a great ASP snippet. I was forced to do some coding in classic ASP, thank you so much for your code, you saved my day! I need to simply generate a random number. :)

Thanks yo!