2008 Winter Scripting Games

Tip of the Week: Generating Random Numbers

Tip of the Week Comic

*
On This Page
Generating Random NumbersGenerating Random Numbers
Generating Random Numbers in PerlGenerating Random Numbers in Perl
Generating Random Numbers in Windows PowerShellGenerating Random Numbers in Windows PowerShell
Generating Random Numbers in VBScriptGenerating Random Numbers in VBScript

Generating Random Numbers

During the inaugural Winter Scripting Games, way back in the year 2006, none of the 8 events required you to generate random numbers. During the 2007 Winter Scripting Games only 1 of the 20 events required you to generate a random number, and that lone event took place in the Advanced Division. In other words, over the past 2 years just 1 out of 28 Scripting Games events (less than 4 percent) required you to work with random numbers. Obviously, then, random numbers will be of minimal use to competitors in the 2008 Winter Scripting Games.

Or will they? The truth is, we can’t tell you exactly what will be required to successfully compete in the Winter Scripting Games; after all, that would spoil all the fun. On the other hand, the sole purpose of this weekly series is to expose you to scripting techniques that might come in handy when the Games begin. So does that mean that random numbers will be used during the upcoming Scripting Games? Sorry; we can’t tell you that. But we can tell you this: it wouldn’t hurt to know how to generate random numbers. And that goes for competitors in the Beginners Division as well as competitors in the Advanced Division.

But you didn’t hear that from us, OK?

Top of pageTop of page

Generating Random Numbers in Perl

In honor of Perl being added to the Scripting Games lineup for 2008, let’s start things off by showing you how to generate a random number in Perl. How do you generate a random number in Perl? Why, like this, of course:

$low = 1;
$high = 100;

print int(rand($high) + $low);

As you can see, it’s not too-terribly complicated to generate a random number in Perl. (And for those of you who worry about such things, well, don’t worry: it’s just as easy to generate random numbers in VBScript and Windows PowerShell.) In our Perl script we start out by assigning the value 1 to a variable named $low and the value 100 to a variable named $high. What’s the significance of those numbers? You got it: we plan to generate a random number between 1 ($low) and 100 ($high).

Note. What if you wanted to generate a random number between 27 and 436? Then simply assign the value 27 (the lowest number in the range) to $low, and 436 (the highest number in the range) to $high. That’s all you have to do.

After setting the range we then call the rand function, passing $high as the function parameter, then adding the value of $low:

rand($high) + $low 

That will give us a random number between 1 and 100, but it could be any random number between 1 and 100 (e.g., 87.328125). In order to limit our generated values to whole numbers, we use the int function to convert our random number to an integer value:

int(rand($high) + $low);

And then we simply call the print function to print the resulting value to the screen:

52

And that’s how you generate a random number in Perl.

OK, OK we know: many of you like to do as little typing as possible. In that case, you can bypass assigning values to variables and simply include those values in your call to the rand function:

print int(rand(100) + 1);

That works, but is a little less flexible and a little harder to understand. When it comes time for the Scripting Games, however, you can do this any way you want: do it in three lines of code, like our original example, or one line of code. That’s entirely up to you.

Um, mind you, we’re not saying that you will need to generate random numbers during the Games. We’re just saying that if you ever need to generate random numbers, for any reason, well, you can use variables or not use variables. It makes no difference to us.

And yes, this is true for both Windows PowerShell and VBScript as well.

Top of pageTop of page

Generating Random Numbers in Windows PowerShell

Speaking of Windows PowerShell, we’re well aware that a few lines of Perl code aren’t of much use to competitors in the PowerShell division. But don’t despair, PowerShell users; here’s how you can generate a set of random numbers that you can call your own:

$low = 1
$high = 100

$a = New-Object Random
$a.Next($low,$high)

With PowerShell we again start off by assigning the values 1 and 100 to variables named $low and $high, respectively. After assigning the variable values we then create an instance of the .NET Framework class System.Random; that’s what we do here:

$a = New-Object Random

Once that’s done we simply call the Next method, passing the variables $low and $high as method parameters:

$a.Next($low,$high)

It’s that easy.

Top of pageTop of page

Generating Random Numbers in VBScript

Two down, one to go. We’re actually going to show you two different ways to generate random numbers in VBScript; we’ll let you decide for yourself which one to use. (Although, having said that, we think the first approach is quite a bit easier.)

Like our PowerShell example, this first VBScript approach also relies on the .NET Framework. If you have the .NET Framework installed on your computer you can generate random numbers using a script similar to this:

intLow = 1
intHigh = 100

Set objRandom = CreateObject("System.Random")
Wscript.Echo objRandom.Next_2(intLow,intHigh)

Don’t you wish all scripting tasks were as easy as generating random numbers? Once again, there’s not much to this. We assign the value 1 to a variable named intLow and the value 100 to a variable named intHigh; by now you should know why we did that. After the variable values have been assigned, we next create an instance of the System.Random class, one of a handful of .NET Framework classes exposed to VBScript scripters. After that we call the oddly-named Next_2 method, passing the two variables as the method parameters:

Wscript.Echo objRandom.Next_2(intLow,intHigh)

Is that going to give us a random number between 1 and 100? Of course it is:

32

You say you don’t have the .NET Framework installed on your computer? That’s fine; VBScript also allows you to generate random numbers the old-fashioned way, using the Rnd function and code similar to this:

intLow = 1
intHigh = 100

Randomize
Wscript.Echo Int((intHigh - intLow + 1) * Rnd + intLow)

You’re right: line 4 is a little crazy-looking, to say the least; that’s just the way VBScript’s Rnd function works. Fortunately, all you have to do is use the same variable names as we did and then you can simply copy that crazy-looking line of code and use it exactly as-is. Note, too that, after generating a random number, we use the Int function to ensure that we get back a whole number, just like we did in our Perl script. Why didn’t we employ some similar function when we used the .NET Framework to generate random numbers? One simple reason: we didn’t have to. The System.Random class gives us back integer values by default.

Oh, and don’t forget about line 3. In that line, we use the Randomize statement to “seed” the random number generator with a unique value: the current system time. (Which, barring an unforeseen fluctuation in the space-time continuum, pretty much has to be a unique value.) If you leave out the Randomize statement your script will work just fine provided you only generate a single random number. What if your script generates more than one random number? Well, at the risk of sounding flippant, it won’t; instead it will simply generate the same number over and over again. (Try it and see for yourself.)

The moral of this story? Always use the Randomize statement before you call the Rnd function. Always.

Which, come to think of it, is a good moral for almost any story.


Top of pageTop of page