Handout 3h

The Basics of Random Numbers*


Introduction

Sometimes it's useful to have the program pick a value "at random". That is to say, have the program generate a value that can't be predicted. In the case of card games, the program might choose any of the 52 playing cards in a standard deck. Or, perhaps, randomly select one of the six sides of a standard die. In the case of video games, a computer-controlled character might have to select a direction in which to move. All of these situations can easily be handled through the use of random numbers.

How Do I Get Random Numbers?

The Visual Basic function Rnd is used to return psuedo-random values. Most computers (perhaps all computers) don't generate truly random values. To be truly random, the value must not be predictable. However, computers use complex mathematical formulas to generate values which look random, but really aren't. Even though the formulas are complex, they're still predictable.

Conceptually, random numbers generated by a computer can be thought of as a very, very long list of values. The computer always returns the values in the same order, but it doesn't always start at the same place in the list. If the list contained a million values, sometimes the computer would return values starting at the first value, but sometimes it might start at the 105,768th value. Because the starting point is unknown and the list is so long, a repeatable pattern is not discernable.

Each time the Rnd function is used, a new psuedo-random value is returned. The values returned are always less than one (1), but greater than or equal to zero (0). That is to say, VB generates random numbers which may be as low as zero (0), but will never reach the value of one (1).
0 <= Rnd < 1

Values in that range may not seem very useful, but there's a simple formula for converting the values returned by Rnd into a more useful range:

Int((HighestValue - LowestValue + 1) * Rnd) + LowestValue
Creating a function that encapsulates this formula will help simplify the generation of psuedo-random values. 
'===============================================
'Rand - Return a random number in a given range.
'
'Parameters:
'  Low  - The lower bounds of the range.
'  High - The upper bounds of the range.
'
'Returns:
'  Returns a random number from Low..High.
'===============================================

Public Function Rand(ByVal Low As Long, _
                     ByVal High As Long) As Long
  Rand = Int((High - Low + 1) * Rnd) + Low
End Function

With this function, random values can easily be generated to simulate such things as dealing cards or rolling dice.
DieValue = Rand(1, 6)
CardValue = Rand(1, 52)

Initializing the Random Numbers

Each time a program is run, the psuedo-random values returned by Rnd are always the same! Obviously, this is very predictable and not random at all. However, VB has also provided the Randomize method.

Use Randomize to initialize VB's random number generator. This is called seeding. To avoid generating the same sequence of psuedo-random values, call Randomize before you call Rnd; but this only has to be done once. A good place to seed the psuedo-random number generator is in the Form's Load event:

Private Sub Form_Load()
  'Initialize the random # generator.
  Randomize
End Sub

Repeating Random Numbers

If Randomize is not called before Rnd, the same series of psuedo-random values will be returned. It's as if VB always starts returning values from the first entry of that conceptual internal list of 1 million psuedo-random values.

There are occasions when it will be desireable to repeat a series of psuedo-random values, but not always from the "first" entry of the internal list of 1 million. To start somewhere else in that conceptual list, call Rnd with a negative parameter, followed by a call to Randomize, passing it the starting point. For example:

'Tell VB to initialize using Randomize's parameter.
Rnd -1
'Tell VB to use 123 as the initialization point (seed).
Randomize 123

By replacing 123 with a different seed value, the starting point into the conceptual list of 1 million psuedo-random values changes, but each time the program is run, the values returned will always be in the same sequence.

Why Would I Want Repeating Random Numbers?

There are at least two good senarios where repeatable sequences of psuedo-random numbers can be beneficial.

First, it's good for testing. Testing programs that use random values is made easier if the sequence of random values is foreknown. If a program makes decisions based on random values and it is know what those values will be, it is easier to predict what decisions the program should make.

Second, certain types of encryption algorythms utilize repeatable random number sequences. By properly seeding the psuedo-random number generator, a repeatable pattern can be generated. Since there are many possible seed values to Randomize, it would be difficult for anyone to guess which one has been chosen.


* from here.