Generate a random number

The following code shows how to generate a random number in ASP in the range 1 through 6 inclusive. To generate random numbers for a different range, simply change the intMinRangeValue to the minimum number and intMaxRangeValue to the maximum number you desire to generate. For example, if you want to generate a random number between 10 and 300, simply set intMinRangeValue to 10 and intMaxRangeValue to 300.

Scripting code

<%
dim shtml, intRandomNumber
Const intMinRangeValue = 1
Const intMaxRangeValue = 6

'get random numbers between intMinRangeValue and intMaxRangeValue inclusive
'Our random number interval range is 1 through 6 because
'intMinRangeValue = 1 and intMaxRangeValue = 6
intRandomNumber = GenerateRandomNumber (intMinRangeValue, _
intMaxRangeValue)

' a variable for holding output
shtml = "A random number between ("
shtml = shtml & intMinRangeValue & ", " & intMaxRangeValue & "): " & intRandomNumber
response.write shtml

function GenerateRandomNumber (intMin, intMax)
' Generate random number between intMin and intMax inclusive
Randomize
GenerateRandomNumber = int((intMax - intMin + 1) * Rnd + intMin)
end function
%>

Output of the above code

A random number between (1, 6): 2