This tutorial will teach how to use the random number generation in VB.NET to create a simple application to roll a 6-sided die. The steps in this can be further applied to include more than 1 die, different sizes of dice, or anything you want to think of that requires a random number generator. For simplicity's sake, I'll only be coding this into a single form, but the same techniques can be applied to custom classes as well.
First things first, let's create a simple form.
Click to view attachment
It doesn't take much for this application, just a button and an output label. Sure you can get fancy and add in images or a combo box to let them choose the number of sides, I'll even give you a screen shot and code snippet at the end of this tutorial showing you how, but for now let's keep it simple.
Note: The name of my output label is lblDiceOutput and the name of my "Roll the Die" button is btnRoll
Okay, now that we have a form in place, let's move on to the simplistic code behind this app.
CODE
Public Class frmD6
Private Sub btnRoll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRoll.Click
Dim oDice As New Random
Dim iDiceResult As Integer = oDice.Next(1, 7)
lblDiceOutput.Text = iDiceResult.ToString
End Sub
End Class
I'll break this down line by line for you so you understand what's going on.
- Public Class frmD6 - This line is the opening of my form, frmD6 is what I decided to name it
- Private Sub btnRoll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRoll.Click - Starts the handler for the click event of btnRoll. Put simply, anything between this and End Sub will happen when you click the "Roll the Die" button.
- Dim oDice As New Random - Creates a new object of the type random. oDice has now become our random number generator.
- Dim iDiceResult As Integer = oDice.Next(1, 7) - This one can be a little hard to grasp at first. First I have to say this. Random is not entirely random. All computers can do in the end is perform math, and through math there are only a few limited ways to create "random" numbers, and then those go in a recurring pattern (it may only start recurring after 64,000 digits or so, but it is recurring). So this kept in mind, what this line does is it creates the iDiceResult variable as an Integer and then it sets it to the next random number in the sequence. 1 and 7 represent the lower and upper bounds respectively, remember when setting this that the second argument will always be greater than the random number generated. In this case we only want the die to go to 6, so the upperbound is 7.
- lblDiceOutput.Text = iDiceResult.ToString - This simply sets the now "random" value stored in iDiceResult to the text property of lblDiceOutput to display what the "dice rolled."
- End Sub - As I mentioned earlier, this closes the handler for the btnRoll.Click event.
- End Class - And this ends my form code altogether.
So now you have a working digital 6-sided-die. Simple wasn't it? Now as I promised, here's the code for if you wanted to add a little flair to your form. I won't be adding images in this case for simplicities sake, but I will be adding a combobox to allow you to select how many sides the die has, and a list box to keep track of past rolls.
The slightly more stylish form:
Click to view attachment
The code behind it:
CODE
Public Class frmD6
Private Sub btnRoll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRoll.Click
If cmbDieSize.Text = String.Empty Then
MessageBox.Show("Please choose the size of die you wish to roll", "Error") 'Quick error handling in case the user tries to roll without selecting a die size
Else
Dim oDice As New Random
Dim iDieSize As Integer = CInt(cmbDieSize.Text)
Dim iDiceResult As Integer = oDice.Next(1, iDieSize + 1)
lstDiceRolls.Items.Add("D" & iDieSize.ToString & ControlChars.Tab & iDiceResult.ToString)
End If
End Sub
Private Sub frmD6_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lstDiceRolls.Items.Add("Size" & ControlChars.Tab & "Roll")
End Sub
End Class
Notes: I kept my form name the same out of laziness. cmbDieSize is the combobox above the roll button containing the die sizes, it was populated in the design view. lstDiceRolls is the listbox on the left of the form that records the rolls.
Well, that's all for this tutorial. Any and all corrections and feedback are welcome (especially since its my first one).
