• Log in or sign up for an account here: https://www.sythe.org/login

Help with random dice

redawsomeness

Forum Addict

Banned
Joined
Jul 28, 2010
Sythe Gold
0
Messages
614
Alright so I was assigned this project in class, but I felt like playing games at the time lol so I decided that I would do it at home for "study material" for the final.

I have the program built, and its working. But, it won't change the numbers every time, like it's not fully randomized or something. The numbers do change every time I do open and close the program though, but it doesn't change during the same time that the program is open.

If anybody could help me, it would be great :)

Here is a quote from the document with all the exercises on it and what he wants us to do on it:
DL ex 3.Write a program that repeatedly "throws" a pair of dice and tallies the number of tosses and the number of tosses that equal (lucky) seven. The program should stop when 100 lucky sevens have been tossed. The program should then report the approximate odds of tossing a lucky seven. The odds will be "1 in" followed by the result of dividing the total number of tosses by the number of sevens tossed. Hint: Use two random numbers, one for each die.

And then here is my source code:
Public Class Form1
Dim intDice1 As Integer
Dim intDice2 As Integer
Dim int7 As Integer
Dim intNot7 As Integer
'Treat intNot7 as if it was intRolled because by the time I had finished typing in the code
'I had noticed that I didn't need this variable so I treated it as if it was intRolled
Dim intDiceAnswer As Integer
Dim intPercentage As Integer
Private Sub btnRoll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRoll.Click
Me.lbl7Answer2.Text = Nothing
Me.lblPercentageAnswer2.Text = Nothing
Me.lblRolledAnswer2.Text = Nothing
Do Until int7 = 100
Randomize()
intDice1 = Int(Rnd() * 6) + 1
intDice2 = Int(Rnd() * 6) + 1
intDiceAnswer = intDice1 + intDice2
If intDiceAnswer = 7 Then
int7 = int7 + 1
intNot7 = intNot7 + 1
ElseIf intDiceAnswer = 1 Then
intNot7 = intNot7 + 1
ElseIf intDiceAnswer = 2 Then
intNot7 = intNot7 + 1
ElseIf intDiceAnswer = 3 Then
intNot7 = intNot7 + 1
ElseIf intDiceAnswer = 4 Then
intNot7 = intNot7 + 1
ElseIf intDiceAnswer = 5 Then
intNot7 = intNot7 + 1
ElseIf intDiceAnswer = 6 Then
intNot7 = intNot7 + 1
ElseIf intDiceAnswer = 8 Then
intNot7 = intNot7 + 1
ElseIf intDiceAnswer = 9 Then
intNot7 = intNot7 + 1
ElseIf intDiceAnswer = 10 Then
intNot7 = intNot7 + 1
ElseIf intDiceAnswer = 11 Then
intNot7 = intNot7 + 1
ElseIf intDiceAnswer = 12 Then
intNot7 = intNot7 + 1
End If
Loop
intPercentage = int7 / intNot7 * 100
Me.lblPercentageAnswer2.Text = intPercentage
Me.lblRolledAnswer2.Text = intNot7
Me.lbl7Answer2.Text = int7
End Sub
End Class
 
Your randomize function is correct, it's something in your huge 'IF' statement. But seeing i can't debug while looking at it on the page and i'm too lazy to setup this project myself i can't help you.

If you want me to help, upload your project to the net and i'll have a look at it then.
 
Bah i got bored and did it for you lol...

Do the following:
1). Open up a new project in VB.NET.
2). Add a Label called Label1 to your Form.
3). Add a Button called Button1 to your Form.
4). Copy and paste the code below to your Button1_Click event:
Code:
        Dim intDie1 As Integer
        Dim intDie2 As Integer
        Dim intLuckyCount As Integer
        Dim intRollCount As Integer

        Do Until intLuckyCount = 100

            intRollCount = intRollCount + 1

            Randomize()
            intDie1 = Int(Rnd() * 6) + 1
            intDie2 = Int(Rnd() * 6) + 1

            If intDie1 + intDie2 = 7 Then
                intLuckyCount = intLuckyCount + 1
            End If

            'You may want to int() +1 the introllcount / intluckycount below so you don't get a decimal number.
            Label1.Text = "1 in " & intRollCount / intLuckyCount & " rolls = 7."
        Loop
5). Test the project. :D

Also this is vb.NET not vb6 ;)
 
Bah i got bored and did it for you lol...

Do the following:
1). Open up a new project in VB.NET.
2). Add a Label called Label1 to your Form.
3). Add a Button called Button1 to your Form.
4). Copy and paste the code below to your Button1_Click event:
Code:
        Dim intDie1 As Integer
        Dim intDie2 As Integer
        Dim intLuckyCount As Integer
        Dim intRollCount As Integer

        Do Until intLuckyCount = 100

            intRollCount = intRollCount + 1

            Randomize()
            intDie1 = Int(Rnd() * 6) + 1
            intDie2 = Int(Rnd() * 6) + 1

            If intDie1 + intDie2 = 7 Then
                intLuckyCount = intLuckyCount + 1
            End If

            'You may want to int() +1 the introllcount / intluckycount below so you don't get a decimal number.
            Label1.Text = "1 in " & intRollCount / intLuckyCount & " rolls = 7."
        Loop
5). Test the project. :D

Also this is vb.NET not vb6 ;)

Thanks! :)

Also I don't use vb6...I use the Visual Studio 2008 Express Edition because thats what we use in school, but I thought they were the same thing anyway.
 
Runescape Gamblingrunescape gambling
Back
Top