How Can I Generate Random Numbers Using a Script?

How Can I Generate Random Numbers Using a Script?

  • Comments 8
  • Likes
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.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • how to write a script ti generate a random number

  • This would NOT work with a lottery program because duplicate numbers can be returned from the randomization logic. You'll need to store previously selected numbers and call the randomization logic until a different number were picked.

    - Captain Obvious

  • This will produce a non repeating random numbers.

    intHighNumber = 24

    intLowNumber = 1

    Dim isUsed(24)

    For i = 1 to 100

       Randomize

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

       If IsUsed(intNumber) <> "Y" Then

           IsUsed(intNumber) = "Y"

           strnum = strnum & " " & Cstr(intNumber)

       End If

       'Wscript.Echo intNumber

    Next

    Msgbox strnum

  • Randomize is meant to be called just once. Once the random number generator has been seeded, it's ready to go. (Otherwise, they would have just built the functionality of Randomize into the Rnd function.)

  • Here is a better Random Number Generator. It allows you to select necessary variables, such as the highest number, the lowest number, and the number of random numbers. It also makes all the random numbers visible on one msgbox instead of several. Yes, numbers may occur twice, but, if  you roll multiple dice, you will most likely get numbers that occur twice.  I can see how the first version would be beneficial for PD, seeing as the number of projects is always the same. But it would be much more generally practical if you could select the highest number, lowest number, and amount of random numbers, without having to manually edit the vbs each time you want to change the variables.  Thank you for reading, Kyle.

    intHighNumber = inputbox("Please enter the highest number.","Random Number Generator")

    intLowNumber = inputbox("Please enter the lowest number.","Random Number Generator")

    intNumbers = inputbox("Please enter the number of random numbers.","Random Number Generator")

    For i = 1 to intNumbers

       Randomize

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

    strnum = strnum& "   " & Cstr(intNumber)

    next

    x = msgbox(strnum,4096,"Random Number Generator")

  • seeing how the number of projects could always be the same*

  • Here is a more comprehensive version

    intHighNumber = inputbox("Please enter the highest number.","Random Number Generator")

    If TypeName(intHighNumber) = "Empty" Then

    wscript.quit

    end if

    If Len(Trim(intHighNumber)) = 0 Then

    altshutdown1 = MsgBox("Nothing Entered.",4096)

    wscript.quit

    end if

    If not isnumeric (intHighNumber) then

    msg2 = msgbox("Not a number.",4096)

    wscript.quit

    end if

    intLowNumber = inputbox("Please enter the lowest number.","Random Number Generator")

    If TypeName(intLowNumber) = "Empty" Then

    wscript.quit

    end if

    If Len(Trim(intLowNumber)) = 0 Then

    altshutdown1 = MsgBox("Nothing Entered.",4096)

    wscript.quit

    end if

    If not isnumeric (intLowNumber) then

    msg2 = msgbox("Not a number.",4096)

    wscript.quit

    end if

    intNumbers = inputbox("Please enter the number of random numbers.","Random Number Generator")

    If TypeName(intNumbers) = "Empty" Then

    wscript.quit

    end if

    If Len(Trim(intNumbers)) = 0 Then

    altshutdown1 = MsgBox("Nothing Entered.",4096)

    wscript.quit

    end if

    If not isnumeric (intNumbers) then

    msg2 = msgbox("Not a number.",4096)

    wscript.quit

    end if

    For i = 1 to intNumbers

       Randomize

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

    strnum = strnum& "   " & Cstr(intNumber)

    next

    x = msgbox(strnum,4096,"Random Number Generator")

    sorry about the length

  • This function will return an array containing n random UNIQUE numbers. If you use it to win the the PowerBall, buy me a beer :-)

       Function GetRandom(lowerbound,upperbound,howmany)

           If upperbound-lowerbound < howmany Then

               GetRandom = array(0)

           Else

               Dim dic,n,num,x

               Set dic = createobject("scripting.dictionary")

               Randomize

               Do while dic.count < howmany

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

                   If not dic.exists(n) Then

                       dic.add n,0

                   End If

               Loop

               ReDim arr(howmany-1)

               x=0

               For each num in dic.keys

                   arr(x) = num

                   x=x+1

               Next

               GetRandom = arr

               Set dic = nothing

          End If

       End Function

         Dim whiteballs,powerball

         whiteballs = GetRandom(1,59,5)

         powerball = GetRandom(1,35,1)

         wscript.echo join(whiteballs," ") & "  " & powerball(0)