VBScript Scripting Techniques | > | Data | > | Random |
The following code uses standard VBScript only to generate a random number in the 1..100 range:
Randomize Wscript.Echo Int( ( 100 - 1 + 1 ) * Rnd + 1 )
The general equation is:
Int( ( upperbound - lowerbound + 1 ) * Rnd + lowerbound )
The following code, borrowed from the Scripting Guys, requires the .NET Framework:
Set objRandom = CreateObject( "System.Random" ) WScript.Echo objRandom.Next_2( 1, 100 )
Looking at this example, the general equation isn't hard to guess:
objRandom.Next_2( lowerbound, upperbound )
Computer generated random numbers are not true random numbers.
They are retrieved from a stored semi-random sequence.
Try the WSH sample without Randomize
, have it retrieve multiple random numbers, and then run the script several times.
The outcome will be the same each time you run the script.
That is why websites like random.org and random.irb.hr exist.
They use atmospheric noise and photonic emission in semiconductors respectively, which is more truly random than anything that computer software can generate.
I wrote 2 functions, a class and a Windows Script Component to retrieve random integers from random.org:
The sample script on the True Random Functions page shows how to call these functions.
The sample script on the True Random Class page shows how to use the class.
The sample script on the True Random Windows Script Component page shows how to use the component.
page last modified: 2016-09-19; loaded in 0.0013 seconds