Wyo VB Lecture Notes
Objective #1: Generate random numbers within
desired ranges using the Rnd method.
- Technically, it is impossible
to generate
a truly random number with a computer since it does not have a mind of
its own to pick numbers at random. The number that the computer generates
must be based on something else such as the number of seconds since midnight. Therefore it is best to describe these numbers as pseudorandom rather than truly random.
- You can use the built-in method Rnd to generate random numbers. The Rnd method returns a decimal number between 0 and 1. Actually, the random number could be 0 exactly but it will not be 1 exactly.
- Using the statement
intDiceRoll = Math.Floor(Rnd() * (Hi - Lo + 1)) + Lo
you can generate a random whole number between and including the values substituted for Lo and Hi and store that random number in the variable intDiceRoll.
- Use the Randomize statement first though in the Form_Load method. This "seeds" the VB random number generator so that your program doesn't generate the same random numbers every time it executes.
- The statement
dblProbability = Rnd()
would generate a random decimal number between 0 and 1 (but not including 1). So you could follow this with an If statement such as
If (dblProbability < 0.5) Then
MessageBox.Show("It is going to snow")
Else
MessageBox.Show("It is NOT going to snow")
End If
to simulate a 50% chance of snow.
Or, you could use the following If statement to simulate a baseball player's at bat where he/she has a 1/3 chance of getting a hit, a 1/3 chance of striking out, and a 1/3 chance of getting a walk to first base
If (dblProbability < 0.333) Then
MessageBox.Show("The baseball player gets a base hit.")
ElseIf (dblProbability < 0.666) Then
MessageBox.Show("The baseball player strikes out")
Else
MessageBox.Show("The baseball player gets a walk to first base.")
End If
- Using the formula without that Math.Floor method can be used to generate a decimal number within a larger range such as between 1 and 100 (but not including the 100) as in
dblProbability = Rnd() * (Hi - Lo + 1) + Lo
which simplifies to
dblProbability = Rnd() * (100 - 1 + 1) + 1
and simplifies further to
dblProbability = Rnd() * 100 + 1
Objective #2: Generate random numbers within desired ranges using Random.
- There is another way in which random numbers can be produced however this method will not be tested on worksheets or exams in this Visual Basic course. You can use the built-in VB class
named Random to generate random numbers.
- The statement
Dim dice As New Random
declares a Random object named dice.
I don't require that you use a specified prefix when naming Random objects.
- The Next method
returns an integer number between 0 and one less than its parameter. For example,
the statement
MessageBox.Show(dice.Next(6))
displays a pseudorandom integer between or including 0 and 5. If you want
to simulate the roll of a dice, then you must add one to the the value
that
is returned by the Next method as in
MessageBox.Show(dice.Next(6) + 1)
- To seed the Random object,
you use the following statement to declare the Random object.
Dim dice As New Random(1)
with some integer seed value specified in the parentheses.
Any number will work fine. The result will be that the string of random values
generated by the Next method will be the
same pattern of random values. This is helpful when testing and debugging
a program.
- Use the nextDouble method to generate a pseudorandom floating-point
value. The following statement will display a random decimal number between
0 inclusive and 1 exclusive meaning that 0 could be a returned value but
1 will not be a possible returned value.
MessageBox.Show(dice.NextDouble())