Active Server Pages offers a cool little function called "Rnd". With Rnd, we can create totally off-the-wall random numbers or we can randomly choose up to a certain number. In the randomization process, we must "Randomize". Before we create a random number, we need to tell the script to randomize so we do:
<% randomize %>
After we've randomized, we can create an off-the-wall freakish number:
You may be wondering why the "rnd" is contained within int(). Having JUST rnd will make the number include decimal numbers (3.49513548 for example). int(rnd) will shave off all of the decimals that may appear after the random number. The large number (9417648295846) is what the random number is being multiplied by.
Now that you know how to create an off-the-wall random number, I will show you how to randomly choose between a certain set of numbers.
In the following example, we will randomly select a number within the range of 1 - 3. This is how it's done:
<% thenumber = int(rnd * 3)+1 %>
This will actually randomly select between 0 and 2 because Visual Basic is just like that, so we added the "+1" to it.
Here is an example that randomly chooses from three background colors for a web page:
<% randomize thenumber = int(rnd * 3) + 1 if thenumber=1 then bgcolor="ffffff" elseif thenumber=2 then bgcolor="00ff00" elseif thenumber=3 then bgcolor="ffff00" end if %> <html><head><title>test</title></head> <body bgcolor="#<%=bgcolor%>"> just testing... </body></html>