Qbasicnews.com
March 28, 2020, 05:03:01 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1] 2 3
  Print  
Author Topic: Dice problem  (Read 9050 times)
hoon
New Member

Posts: 3


« on: April 07, 2004, 12:24:12 AM »

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.
Logged
relsoft
*/-\*
*****
Posts: 3927



WWW
« Reply #1 on: April 07, 2004, 03:08:42 AM »

Probability=Times a particular event is likely to occur/Max Events
Logged

y smiley is 24 bit.


Genso's Junkyard:
http://rel.betterwebber.com/
1000101
Ancient Guru
****
Posts: 519



WWW
« Reply #2 on: April 07, 2004, 06:39:57 AM »

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.
Logged

Life is like a box of chocolates', hrm, WTF, no it isn't, more like, 'life is like a steaming pile of horse crap.'
Agamemnus
x/ \z
*****
Posts: 3491



« Reply #3 on: April 07, 2004, 07:42:52 PM »

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%
Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
hoon
New Member

Posts: 3


« Reply #4 on: April 07, 2004, 11:24:57 PM »

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.
Logged
1000101
Ancient Guru
****
Posts: 519



WWW
« Reply #5 on: April 08, 2004, 02:16:02 AM »

Quote from: "Agamemnus"

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(Cool * 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.
Logged

Life is like a box of chocolates', hrm, WTF, no it isn't, more like, 'life is like a steaming pile of horse crap.'
Plasma
Na_th_an
*****
Posts: 1770


WWW
« Reply #6 on: April 08, 2004, 02:20:38 AM »

Not if you select "All Files" for "Save as type" and then put a .bas extension on the filename.
Logged
1000101
Ancient Guru
****
Posts: 519



WWW
« Reply #7 on: April 08, 2004, 02:26:09 AM »

I've found it doesn't believe that ;P
Logged

Life is like a box of chocolates', hrm, WTF, no it isn't, more like, 'life is like a steaming pile of horse crap.'
Plasma
Na_th_an
*****
Posts: 1770


WWW
« Reply #8 on: April 08, 2004, 02:33:20 AM »

It works for me...
Logged
Anonymous
Guest
« Reply #9 on: April 08, 2004, 08:17:05 AM »

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

"whatever.bas"

that always works for me
Logged
oracle
*/-\*
*****
Posts: 3652



WWW
« Reply #10 on: April 08, 2004, 07:38:04 PM »

Quote from: "Agamemnus"
102.88%


LOL

Quote from: "1000101"
Note, however, notepad is a pain and *always* adds .txt to the filename.


I was gonna say, it works for me as well  :Huh:
Logged

hoon
New Member

Posts: 3


« Reply #11 on: April 08, 2004, 11:35:19 PM »

Here is the final code I used:
Code:

RANDOMIZE TIMER
count = 0
bc = 0
FOR t = 0 TO 1000
count = 0
        a = INT(RND * 6) + 1
           IF (a = 6) THEN
           count = count + 1
           END IF
        b = INT(RND * 6) + 1
           IF (b = 6) THEN
           count = count + 1
           END IF
        c = INT(RND * 6) + 1
           IF (c = 6) THEN
           count = count + 1
           END IF
        d = INT(RND * 6) + 1
           IF (d = 6) THEN
           count = count + 1
           END IF
        e = INT(RND * 6) + 1
           IF (e = 6) THEN
           count = count + 1
           END IF
        f = INT(RND * 6) + 1
           IF (f = 6) THEN
           count = count + 1
           END IF
        g = INT(RND * 6) + 1
           IF (g = 6) THEN
           count = count + 1
           END IF
        h = INT(RND * 6) + 1
           IF (h = 6) THEN
           count = count + 1
           END IF
        i = INT(RND * 6) + 1
                IF (i = 6) THEN
                count = count + 1
                END IF
        IF (count = 4) THEN
        bc = bc + 1
        END IF
NEXT t
PRINT bc


My teacher said it was accurate enough :/
I would have used 1000101's code, but we dont have visual basic at the school, although I have it at home.
THANKS FOR THE HELP
Logged
whitetiger0990
__/--\__
*****
Posts: 2964



WWW
« Reply #12 on: April 08, 2004, 11:50:23 PM »

were all those different variables nessacary (cant spell)

couldn't you have just loops through the randomize checking thngy 9 or so times?
Logged


[size=10]Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.[/size]
1000101
Ancient Guru
****
Posts: 519



WWW
« Reply #13 on: April 09, 2004, 05:53:40 AM »

The code I wrote works in QB.

In fact, I had to modify it to work in VB ;P
Logged

Life is like a box of chocolates', hrm, WTF, no it isn't, more like, 'life is like a steaming pile of horse crap.'
Agamemnus
x/ \z
*****
Posts: 3491



« Reply #14 on: April 09, 2004, 04:25:14 PM »

why log?

I don't think it's log.

Anyways I still did it wrong anyways.

Probability of getting 5 of the same sides out of 8 dice, and having it be only one side is:

total possible combinations of the same side ("4") / total possible combinations.

total possible combinations = 6^8 = 1679616

total possible combinations of getting "4" in exactly 5/8 situations = total possible combinations of getting a single number in 5/8 situations divided by 6.


--------

5/8 represented in 0/1 form:
00011111
etc.

three zeroes positioned in any spot.

First move the first zero around and leave the other two zeroes there. 6 combinations.
Then the others um let's say 5 and 4 more combinations.
so:
5*4*6/6=20

so it's (20/1679616) not (1/6)^5*8..

So it's:

1000*20/1679616

Um yeah,

1.2%

But this gives 4...

Code:

CLS

RANDOMIZE TIMER

FOR k% = 1 TO 50
FOR i% = 1 TO 1000
FOR j% = 1 TO 8
IF INT(RND * 6) = 0 THEN isfour% = isfour% + 1
NEXT j%
IF isfour% = 5 THEN count% = count% + 1
isfour% = 0
NEXT i%
NEXT k%

PRINT count% \ k%
Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Pages: [1] 2 3
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!