How Can I Generate Random Numbers Using a Script?

Hey, Scripting Guy! Question

Hey, Scripting Guy! Our auditors have suggested that we randomly pick projects to examine and ensure that each project was completed and signed off per company policy. Because these projects are numbered sequentially I thought maybe we could write a script that would randomly select the project numbers for us to audit. Can you help us with that?

— PD

SpacerHey, Scripting Guy! AnswerScript Center

Hey, PD. You know, if you were looking for organized, consistent, and reliable we’d have to say, “No, sorry, we can’t help you.” But seeing as how you’re looking for random and unpredictable, well, you came to the right place.


As it turns out, VBScript has a built-in function (with the catchy name Rnd) designed solely to return random numbers. Let’s take a look at a script that generates 5 random numbers, each one between 1 and 100. We’ll then explain how the script works:

intHighNumber = 100
intLowNumber = 1

For i = 1 to 5
Randomize
intNumber = Int((intHighNumber – intLowNumber + 1) * Rnd + intLowNumber)
Wscript.Echo intNumber
Next


As you can see, we begin by assigning values to two different variables: intHighNumber (the highest number in our range) and intLowNumber (the lowest number in our range). What if we wanted to generate random numbers between 37 and 956? No problem: we’d simply adjust the values of intHighNumber and intLowNumber:

intHighNumber = 956
intLowNumber = 37

Next we set up a For Next loop that loops around 5 times. Inside the loop we find the following three lines of code:

Randomize
intNumber = Int((intHighNumber – intLowNumber + 1) * Rnd + intLowNumber)
Wscript.Echo intNumber

The first line – consisting solely of the Randomize statement – is very important. This function uses the system time to provide a “seed” value to the Rnd function. Because the system time will always be different this means that the Rnd function will be more likely to generate a truly random number. In fact, try running this script, one that doesn’t use the Randomize statement:

intHighNumber = 100
intLowNumber = 1

intNumber = Int((intHighNumber – intLowNumber + 1) * Rnd + intLowNumber)
Wscript.Echo intNumber


No matter how many times you run this you’ll get the same “random” number. (On our test computer, we get 71 each time.)


Our next line of code actually generates the random number:

intNumber = Int((intHighNumber – intLowNumber + 1) * Rnd + intLowNumber)

The Rnd function has an algorithm it uses to generate random numbers; all we have to do is plug our two variables (intHighNumber and intLowNumber) into the equation. In addition, we use the Int function to ensure that we get back an integer rather than a decimal number. It’s an odd-looking little line of code, but you don’t have to worry about it; just plug the variables into the right spots and have at it.


That’s pretty much it; we echo back the returned number and then loop around and generate the next random number. When we’re all done we should get back a list of numbers similar to this:

82
15
92
32
13

By the way, could you use this approach to generate numbers for playing the lottery? Yes. Would that make it more likely that you’d pick the winning numbers? Well, let’s put it this way: the Scripting Guys still have to come in to work each and every day. You can decide for yourself whether we’d do that had we written a script that would win us the $100 million PowerBall jackpot.