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 generates and returns a decimal number between 0 and 0.9999.
Examples:
MessageBox.Show(Rnd()) ' 0.345 could be displayed
MessageBox.Show(Rnd()) ' 0.891 could be displayed
MessageBox.Show(Rnd()) ' 0.003 could be displayed
MessageBox.Show(Rnd()) ' 0.999 could be displayed
- Typically, you want an integer to be generated as a random number rather than a decimal number. Here's an algorithm for turning the decimal number that Rnd generates into an integer. The following examples show how you can simulate the roll of a regular 6-sided dice with the Rnd method:
- multiply the value that Rnd generates by 6
- chop off the decimal places
- add 1
Study the following examples to see the algorithm in action:
Dim num As Double = 0
num = Rnd() ' 0.000 is the smallest possible pseudorandom number that Rnd generates
MessageBox.Show(num) ' 0.000
num = num * 6 ' num is now 0.000 since 0.000 * 6 = 0.000
MessageBox.Show(num) ' 0.000
num = Math.Floor(num) ' the decimal places in 0.000 are truncated and "cut off"
MessageBox.Show(num) ' 0
num = num + 1 ' 1 is added since a dice typically displays the numbers 1 through 6 not 0 through 5
MessageBox.Show(num) ' 1 is the dice roll
Dim num As Double = 0
num = Rnd() ' 0.001 could be a pseudorandom number that Rnd generates
MessageBox.Show(num) ' 0.001
num = num * 6 ' num is now 0.006 since 0.001 * 6 = 0.006
MessageBox.Show(num) ' 0.006
num = Math.Floor(num) ' the decimal places in 0.006 are truncated and "cut off"
MessageBox.Show(num) ' 0
num = num + 1 ' 1 is added since a dice typically displays the numbers 1 through 6 not 0 through 5
MessageBox.Show(num) ' 1 is the dice roll
Dim num As Double = 0
num = Rnd() ' 0.555 is a number near the middle of the range of possible pseudorandom numbers that Rnd generates
MessageBox.Show(num) ' 0.555
num = num * 6 ' num is now 3.33 since 0.555 * 6 = 3.33
MessageBox.Show(num) ' 3.33
num = Math.Floor(num) ' the decimal places in 3.33 are truncated and "cut off"
MessageBox.Show(num) ' 3
num = num + 1 ' 1 is added since a dice typically displays the numbers 1 through 6 not 0 through 5
MessageBox.Show(num) ' 4 is the dice roll
Dim num As Double = 0
num = Rnd() ' 0.999 is the largest possible pseudorandom number that Rnd can generate
MessageBox.Show(num) ' 0.999
num = num * 6 ' num is now 5.994 since 0.999 * 6 = 5.994
MessageBox.Show(num) ' 5.994
num = Math.Floor(num) ' the decimal places in 5 are truncated and "cut off"
MessageBox.Show(num) ' 5
num = num + 1 ' 1 is added since a dice typically displays the numbers 1 through 6 not 0 through 5
MessageBox.Show(num) ' 6 is the dice roll
- The formula Math.Floor(Rnd() * (High - Low + 1)) + Low
can be used to generate a random whole number between and including the values substituted for Low and High
For example, if you want to generate a random number between 1 and 6 then you plug 1 in for Low in the formula above and you plug in 6 for High
diceRoll = Math.Floor(Rnd() * (6 - 1 + 1)) + 1 which simplies to ....
diceRoll = Math.Floor(Rnd() * (5 + 1)) + 1 which simplies to ....
diceRoll = Math.Floor(Rnd() * 6) + 1
Or, if you want to generate a random number between 10 and 20 then you plug 10 in for Low you plug in 20 for High
diceRoll = Math.Floor(Rnd() * (20 - 10 + 1)) + 10 which simplies to ....
diceRoll = Math.Floor(Rnd() * (10 + 1)) + 10 which simplies to ....
diceRoll = Math.Floor(Rnd() * 11) + 10
- The statement
Dim probability As Double = 0.0
probability = Rnd()
would store a random decimal number between 0 and 0.99999 into the variable probability. So the If statement
If (probability < 0.5) Then
MessageBox.Show("Heads")
Else
MessageBox.Show("Tails")
End If
simulates the flip of a coin.
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 (probability < 0.333) Then
MessageBox.Show("The baseball player gets a base hit.")
ElseIf (probability < 0.666) Then
MessageBox.Show("The baseball player strikes out")
Else
MessageBox.Show("The baseball player gets a walk to first base.")
End If
- Use the Randomize statement first though in the Form1_Load method. This "seeds" the VB random number generator so that your program doesn't generate the same pattern of random numbers every time it runs.
Objective #2: Use random numbers to simulate dice rolls and computer interactions with a player in a game.
- In some games, you roll one dice. So you may end up using the code segment
Private Sub btnRoll_Click(...)
Dim diceRoll As Integer = 0
diceRoll = Math.Floor(Rnd() * 6) + 1
MessageBox.Show(diceRoll)
End Sub
If you need to roll two dice, then you might use code like this
Private Sub btnRoll_Click(...)
Dim diceRoll1 As Integer = 0
Dim diceRoll2 As Integer = 0
Dim total As Integer = 0
diceRoll1 = Math.Floor(Rnd() * 6) + 1
diceRoll2 = Math.Floor(Rnd() * 6) + 1
total = diceRoll1 + diceRoll2
MessageBox.Show(total)
End Sub
If you need to simulate a baseball player's turn at bat, you might use something like this
Private Sub btnAtBat_Click(...)
Dim batterAction As Integer = 0
batterAction = Rnd()
If (batterAction < .200) Then
MessageBox.Show("single")
ElseIf (batterAction < .300) Then
MessageBox.Show("double")
ElseIf (batterAction < .320) Then
MessageBox.Show("triple")
ElseIf (batterAction < .350) Then
MessageBox.Show("home run")
ElseIf (batterAction < .500) Then
MessageBox.Show("walk")
ElseIf (batterAction < .750) Then
MessageBox.Show("grounded out or pop fly out")
Else
MessageBox.Show("strikeout")
End If
End Sub