Qbasicnews.com

Full Version: Dice problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4 5
Hey everybody, this is my first post.

The teacher in my computer programming class at the high school asked us to write a program that would find the number of times you would get five sides of 4 on eight 6-sided dice when rolled 1,000 times. Obviously each roll would be random. I wrote the program I thought would work, but alas it didn't and I ran out of time.

If anyone could show me a program that would work for this, it would be really awesome. Don't spend too much time on it though, it isn't too important.

Thanks.
Probability=Times a particular event is likely to occur/Max Events
Rel: There is a bell curve here to contend with. It's not a linear ramp.

hoon:

Code:
Dim D(1 To 8) As Integer

Dim C(1 To 5) As Integer

Dim Matches As Long
Dim Total As Long

Total = 0
Matches = 0
D(1) = 0
Do
   D(1) = D(1) + 1                                                         '' All possable sides of die 1
   D(2) = 0
   Do
      D(2) = D(2) + 1                                                      '' All possable sides of die 2
      D(3) = 0
      Do
         D(3) = D(3) + 1                                                   '' All possable sides of die 3
         D(4) = 0
         Do
            D(4) = D(4) + 1                                                '' All possable sides of die 4
            D(5) = 0
              Do
                 D(5) = D(5) + 1                                             '' All possable sides of die 5
                 D(6) = 0
                  Do
                     D(6) = D(6) + 1                                          '' All possable sides of die 6
                     D(7) = 0
                      Do
                         D(7) = D(7) + 1                                       '' All possable sides of die 7
                         D(8) = 0
                          Do
                              D(8) = D(8) + 1                                    '' All possable sides of die 8

                           Total = Total + 1                                  '' Increase total combination count

                                C(1) = 0
                                Do
                                   C(1) = C(1) + 1                                 '' All possable dice
                                   C(2) = C(1)
                                   Do
                                      C(2) = C(2) + 1                              '' All possable dice
                                      C(3) = C(2)
                                      Do
                                         C(3) = C(3) + 1                           '' All possable dice
                                         C(4) = C(3)
                                         Do
                                            C(4) = C(4) + 1                        '' All possable dice
                                              C(5) = C(4)
                                              Do
                                                 C(5) = C(5) + 1                     '' All possable dice

                                       '' Check dice 1=2, 1=3, 1=4, 1=5
                                                If (D(C(1)) = D(C(2))) And (D(C(1)) = D(C(3))) And (D(C(1)) = D(C(4))) And (D(C(1)) = D(C(5))) Then
                                                    '' Check die 1 = 4
                                                     If D(1) = 4 Then Matches = Matches + 1
                                               End If

                                               Loop Until C(5) = 8
                                           Loop Until C(4) = 7
                                        Loop Until C(3) = 6
                                   Loop Until C(2) = 5
                                Loop Until C(1) = 4

                         Loop Until D(8) = 6
                      Loop Until D(7) = 6
                   Loop Until D(6) = 6
            Loop Until D(5) = 6
         Loop Until D(4) = 6
      Loop Until D(3) = 6
   Loop Until D(2) = 6
Loop Until D(1) = 6

Print "Of"; Total; "combinations, statistically, 5 of 8 dice equalling 4 will match"; Matches; "times."

End

Compiling this in VB6 to take advantage of my Athlon, it only took 10 seconds (10.3584, but who cares? Wink )

Anyway, this is the output:

Code:
Of 1679616 combinations, statistically, 5 of 8 dice equalling 4 will match 12096 times.

A 'random' version can be done as follows (but it's inaccurate):

Code:
Dim D(1 To 8) As Integer

Dim C(1 To 5) As Integer

Dim Matches As Long
Dim Total As Long

Randomize Timer                                       '' Seed random number generator

Matches = 0
For Total = 0 To 999
   D(1) = Int(Rnd * 6) + 1
   D(2) = Int(Rnd * 6) + 1
   D(3) = Int(Rnd * 6) + 1
   D(4) = Int(Rnd * 6) + 1
   D(5) = Int(Rnd * 6) + 1
   D(6) = Int(Rnd * 6) + 1
   D(7) = Int(Rnd * 6) + 1
   D(8) = Int(Rnd * 6) + 1

    C(1) = 0
    Do
       C(1) = C(1) + 1                                 '' All possable dice
       C(2) = C(1)
       Do
          C(2) = C(2) + 1                              '' All possable dice
          C(3) = C(2)
          Do
             C(3) = C(3) + 1                           '' All possable dice
             C(4) = C(3)
             Do
                C(4) = C(4) + 1                        '' All possable dice
                  C(5) = C(4)
                  Do
                     C(5) = C(5) + 1                     '' All possable dice

                  '' Check dice 1=2, 1=3, 1=4, 1=5
                       If (D(C(1)) = D(C(2))) And (D(C(1)) = D(C(3))) And (D(C(1)) = D(C(4))) And (D(C(1)) = D(C(5))) Then
                           '' Check die 1 = 4
                            If D(1) = 4 Then Matches = Matches + 1
                   End If

                   Loop Until C(5) = 8
               Loop Until C(4) = 7
            Loop Until C(3) = 6
       Loop Until C(2) = 5
    Loop Until C(1) = 4

Next Total

Print "Of"; Total; "combinations, statistically, 5 of 8 dice equalling 4 will match"; Matches; "times."

End

Running it several times, I got the following results:

Code:
Of 1000 combinations, statistically, 5 of 8 dice equalling 4 will match 19 times.
Of 1000 combinations, statistically, 5 of 8 dice equalling 4 will match 16 times.
Of 1000 combinations, statistically, 5 of 8 dice equalling 4 will match 3 times.
Of 1000 combinations, statistically, 5 of 8 dice equalling 4 will match 9 times.
Of 1000 combinations, statistically, 5 of 8 dice equalling 4 will match 15 times.
Of 1000 combinations, statistically, 5 of 8 dice equalling 4 will match 1 times.
Of 1000 combinations, statistically, 5 of 8 dice equalling 4 will match 2 times.

As you can see, it is not accurate at all.
Quote:The teacher in my computer programming class at the high school asked us to write a program that would find the number of times you would get five sides of 4 on eight 6-sided dice when rolled 1,000 times. Obviously each roll would be random. I wrote the program I thought would work, but alas it didn't and I ran out of time

Easier way...hm.....

Probability of getting 1 side of 4 on a 6-sided dice in 1000 rolls:
n# = 1000
n# = n# * (1/6)


Probability of getting 1 side of 4 on 8 6-sided dice:
n# = 1000
n# = n# * (1/6) * 8

Probability of getting 5 sides of 4 on 8 6-sided dice:
Code:
n# = 1000
n# = n# * (1/6)^5 * 8
PRINT n#

102.88%
Thanks people.

1000101:
Your second reply (the less accurate one) was closer to what I was trying to do. But somehow I must have gotten mixed up with the loops at the end. But I know how to do it now! Mucho gracias, master.
Quote:Easier way...hm.....

Probability of getting 1 side of 4 on a 6-sided dice in 1000 rolls:
n# = 1000
n# = n# * (1/6)


Probability of getting 1 side of 4 on 8 6-sided dice:
n# = 1000
n# = n# * (1/6) * 8

Probability of getting 5 sides of 4 on 8 6-sided dice:
Code:
n# = 1000
n# = n# * (1/6)^5 * 8
PRINT n#

102.88%

Wrong-o ;P

You're produced the result of 1.0288 in 1000.

A closer aproximation is

((log(8) * 1/6) ^ 5) * 1000

Yielding ~7.725, the actual odds in 1000 are ~7.202

hoon:

Just cut'n'paste the code into a windows text editor (notepad) and then save it to disk. You can then load it in QB.

Note, however, notepad is a pain and *always* adds .txt to the filename.
Not if you select "All Files" for "Save as type" and then put a .bas extension on the filename.
I've found it doesn't believe that ;P
It works for me...

Anonymous

that works, but all you really have to do is put it in quotes.

"whatever.bas"

that always works for me
Pages: 1 2 3 4 5