There are certain times when you'd like an ASP script to randomly choose to do one of N
tasks. One can use the
Randomize statement and the
in VBScript to select a random number. In fact, this is the only way to generate truly
random numbers. But sometimes generating a purely random number isn't that important, you
would just like to do 1 of N things, where each of the N events are equally probable.
For examle, on the 4GuysFromRolla.com homepage I have a space reserved to place a link to one of my two books: Designing Active Server Pages and Sams Teach Yourself ASP 3.0 in 21 Days. I wanted to randomly have each book displayed... however, I am not too concerned about true randomness and am more interested in having each book shown with equal probability.
It struck me that a very easy thing to do would be to determine the current time (using
Time function), grab the seconds (using the
Mod 2 the result. The
operator is used like:
Number2 and returning the
remainder. Here is a simple chart of
So, to give two events the same probability, I simply did:
That's all there is to it! A quick and dirty way to act on one of two events with equal probability!
|Is that Number Even or Odd?|
|Return to user tips...|