Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBScript - How to Generate a Random Number, then an If-Statement to Use this Number to Pick an Option

I would like to know how to, (In VBScript) generate a random number that would not be the same on a different computer, and then use that number and perhaps some If-Statements so that one of 10 possible options can be activated, eg.

If (A random number between 1 - 10, eg. 2) then (Continue on part of script then wscript.quit)
Else if (A different number, eg. 7) then (continue on to different part of script then wscript.quit)

etc.

So that I would have 10 different options for the script to choose randomly.

Is this possible? If so then would someone be able to compile an example of this so I can put my own script in and use it? Thanks to any answers!

like image 445
CerealKiller Avatar asked Aug 18 '13 06:08

CerealKiller


People also ask

How will you get a random number between 0 and 1 in vbscript?

The Rnd function returns a random number. The number is always less than 1 but greater or equal to 0.

How do you generate a random integer in VB?

Example. This example uses the Rnd function to generate a random integer value from 1 to 6. Dim MyValue As Integer MyValue = Int((6 * Rnd) + 1) ' Generate random value between 1 and 6.


2 Answers

You need randomize and rnd.
int(rnd * n) + 1 evaluates to an integer number between 1 and n.
And you might use select case... here as well, try this:

dim r
randomize
r = int(rnd*10) + 1
select case r
    case 2
        '... 

    case 7
        '... 

end select
like image 87
KekuSemau Avatar answered Nov 23 '22 11:11

KekuSemau


And If you would rand from min to max:

Dim max,min,rand
max=54
min=23
Randomize
rand = Int((max-min+1)*Rnd+min)
WScript.Echo rand
like image 30
Krzysztof Gapski Avatar answered Nov 23 '22 10:11

Krzysztof Gapski