Random numbers

Posted by Boris on Thu 26 Jul 2001 10:09 AM — 2 posts, 13,098 views.

#0
I wouldn't be surprised if this was already on the list... but a scripting function for generating a random number across a given range would be helpful... or does this already exist?
Australia Forum Administrator #1
There is a VBscript function "rnd" that will return random numbers. Here is some stuff from the manual:

The Rnd function returns a value less than 1 but greater than or equal to 0. The value of number determines how Rnd generates a random number:

  • If number is less than zero Rnd generates the same number every time, using number as the seed.
  • If number is greater than zero Rnd generates the next random number in the sequence.
  • If number is equal to zero Rnd generates the most recently generated number.
  • If number is not supplied Rnd generates the next random number in the sequence.



For any given initial seed, the same number sequence is generated because each successive call to the Rnd function uses the previous number as a seed for the next number in the sequence.

Before calling Rnd, use the Randomize statement without an argument to initialize the random-number generator with a seed based on the system timer.

To produce random integers in a given range, use this formula:



Int((upperbound - lowerbound + 1) * Rnd + lowerbound)


Here, upperbound is the highest number in the range, and lowerbound is the lowest number in the range.





Note To repeat sequences of random numbers, call Rnd with a negative argument immediately before using Randomize with a numeric argument. Using Randomize with the same value for number does not repeat the previous sequence.