Qbasicnews.com

QBasic => QB Discussion & Programming Help => Topic started by: hoon on April 07, 2004, 12:24:12 AM



Title: Dice problem
Post by: hoon 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.


Title: Dice problem
Post by: relsoft on April 07, 2004, 03:08:42 AM
Probability=Times a particular event is likely to occur/Max Events


Title: Dice problem
Post by: 1000101 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? ;) )

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.


Title: Dice problem
Post by: Agamemnus 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%


Title: thanks
Post by: hoon 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.


Title: Dice problem
Post by: 1000101 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(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.


Title: Dice problem
Post by: Plasma 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.


Title: Dice problem
Post by: 1000101 on April 08, 2004, 02:26:09 AM
I've found it doesn't believe that ;P


Title: Dice problem
Post by: Plasma on April 08, 2004, 02:33:20 AM
It works for me...


Title: Dice problem
Post by: Anonymous 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


Title: Dice problem
Post by: oracle 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  :???:


Title: Final Code
Post by: hoon 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


Title: Dice problem
Post by: whitetiger0990 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?


Title: Dice problem
Post by: 1000101 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


Title: Dice problem
Post by: Agamemnus 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%


Title: Dice problem
Post by: TheBigBasicQ on April 09, 2004, 04:59:12 PM
Quote from: "Agamemnus"
102.88%


:rotfl:. How did you actually post that? I mean a probability of 100% means you will get that result every time you roll the dice and a probability over 100% is just absurd =P.

Heres my logic:
P(getting the particular side on a 6 sided dice when rolled once) = 1/6
P(5 of 8 dice getting the particular side when rolled once) = 1/6 ^ 5
Thus when the 5 dice are rolled 1000 times the probability would be = 0.12861

PS: P(foo) is read as Probability of foo...

[edit] I too ran a program similar to yours and got the experimental probability as 4.39


Title: Dice problem
Post by: 1000101 on April 09, 2004, 07:55:24 PM
Quote from: "Agamemnus"
why log?

I don't think it's log.


Well, you need to understand logarithms.  Every iteration of a logarithm, the value gets smaller.  For every die you add, the chances of getting the same number increase by 1/6.  The odds of gettings 5 sides the same are definitely *not* 5/8.

Quote from: "Agamemnus"
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%


The problem with that is that if more then five sides are four, then it doesn't count them and it should.

If it's 6 sides, there there are 2 sets of 5 combinations.

More accurate code:

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% + isfour% - 4
isfour% = 0
NEXT i%
NEXT k%

PRINT count% \ k%


Title: Dice problem
Post by: TheBigBasicQ on April 10, 2004, 04:00:31 AM
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.


Read the question properly. It clearly states 5 sides. If they wanted  5 or more sides then they would have used the phrase "atleast 5 sides...".

Atleast thats the way we were taught to interpret the probability questions.

Default word = exactly
Atleast = greater than or equal to
Not more than = less than or equal to


Title: Dice problem
Post by: Agamemnus on April 10, 2004, 11:07:39 AM
It's not log.

The fact that it's at least 5 sides complicates the matter.

If it said "all of the squares" then it would be a lot simpler.

See my analysis and try to get a different number..


Title: Dice problem
Post by: TheBigBasicQ on April 10, 2004, 12:08:20 PM
For atleast 5 sides the solution would differ:

Case i: Exactly five sides
x x x x x o o o
8C5 = 56

Case ii: Six sides
x x x x x x o o
8C6 = 28

Case iii: Seven sides
x x x x x x x o
8C7 = 8

Case iv: Eight sides
x x x x x x x x
8C8 = 1

Thus total no.of occurances of the given condition = 56 * 28 * 8 * 1 = 12,544

Max. combinations = 8P8 = 40,320

P = 12,544 / 40,320 = 0.311111.....

Thus no.of occurances in 1000 trials = 311.11

Is this correct or I suck at probability?  :lol:


Title: Dice problem
Post by: Agamemnus on April 10, 2004, 04:35:24 PM
Figured it out.

5: 7000 combinations.
5 or more: 7741 combinations.

1000*7000/1679616 = 4

Code:

FOR i1% = 1 TO 6: FOR i2% = 1 TO 6: FOR i3% = 1 TO 6: FOR i4% = 1 TO 6
FOR i5% = 1 TO 6: FOR i6% = 1 TO 6: FOR i7% = 1 TO 6: FOR i8% = 1 TO 6
tot% = 0
IF i1% = 1 THEN tot% = tot% + 1
IF i2% = 1 THEN tot% = tot% + 1
IF i3% = 1 THEN tot% = tot% + 1
IF i4% = 1 THEN tot% = tot% + 1
IF i5% = 1 THEN tot% = tot% + 1
IF i6% = 1 THEN tot% = tot% + 1
IF i7% = 1 THEN tot% = tot% + 1
IF i8% = 1 THEN tot% = tot% + 1
IF tot% >= 5 THEN n% = n% + 1
NEXT i8%, i7%, i6%, i5%, i4%, i3%, i2%, i1%
PRINT n%


Title: Dice problem
Post by: 1000101 on April 10, 2004, 04:37:53 PM
Quote from: "TheBigBasicQ"
Read the question properly. It clearly states 5 sides. If they wanted  5 or more sides then they would have used the phrase "atleast 5 sides...".


Maybe you should think about it like this:

Take the dice after they roll, without using a set of 5 more then once, how many 5-die combinations of those 8 dice will have all sides egualling 4:

x x x x x x o o

So, we have 2 sets of 5 sides equalling four:

|x x x x x| x o o
x |x x x x x| o o

And *that* is how I interpreted it.

Quote from: "Agamemnus"
It's not log.


You're right, it's not.  But, for an aproximation, log gets a result a hell of a lot closer then your *highly* inaccurate method.


Title: Dice problem
Post by: relsoft on April 10, 2004, 11:50:46 PM
Bell curve?  You mean "skewness"?

ie.  Right=<0, Left>0, 0 = no skewness


Title: Dice problem
Post by: 1000101 on April 13, 2004, 05:31:23 PM
Now, I mean a bell curve?  Pass grade 10 Algebra and you will understand ;P


Title: Dice problem
Post by: Agamemnus on April 13, 2004, 05:36:06 PM
There is no bell curve and there is no log.


Title: Dice problem
Post by: 1000101 on April 13, 2004, 11:21:46 PM
There *is* a bell curve, learn some math.


Title: Dice problem
Post by: speedlemon on April 13, 2004, 11:34:35 PM
there is also a french curve that i used in art class once.


Title: Dice problem
Post by: LooseCaboose on April 14, 2004, 01:59:57 AM
Probability was never my strong point, but I use a different method and get different results. To illustrate I'll use a situation with 4 dice where we want exactly 2 of the dice to be fours. The other two dice can be anything except the value four. He are all the possible combinations (x's mark the values that are anything but 4).
Code:

4 4 x x
4 x 4 x
4 x x 4
x 4 4 x
x 4 x 4
x x 4 4


So there a six combinations, this can be worked out for any number using the combinations formula:
Code:

  combs = n! / n! * (n - k)!


Where n is the total number of dice and k is the number of faces we are interested in.

For each of the combinations above, we need to fill in the x's, the x's can be any number except 4 so on a six sided dice there are 5 possible values for each x. In the above example that gives as 5 * 5 for each combination for a total of 150 possible situations where there are 2 fours from 4 dice.

For the 8 dice, 4 fours situation we get:
Code:

  total combinations = 6 ^ 8 =  1679616
 
  combs of four 4's = 8C4 = 70
   
  total combs of four 4's = 8C4 * (5 ^ 4) = 43750

  p(four 4's) = 43750/1679616 = 0.026 = 2.6%


So in 1000 rolls you could expect to get exactly four sides showing 4's around about 2.6%, which is about 26 of the rolls.


Title: Dice problem
Post by: LooseCaboose on April 14, 2004, 02:47:05 AM
And proof by experiment (quick and dirty method):
Code:

dim dice(8)

total = 0
fours = 0

FOR i1 = 1 TO 6

  FOR i2 = 1 TO 6
    FOR i3 = 1 TO 6
      FOR i4 = 1 TO 6
        FOR i5 = 1 TO 6
          FOR i6 = 1 TO 6
            FOR i7 = 1 TO 6
              FOR i8 = 1 TO 6

                dice(1) = i1
                dice(2) = i2
                dice(3) = i3
                dice(4) = i4
                dice(5) = i5
                dice(6) = i6
                dice(7) = i7
                dice(8) = i8

                total = total + 1

                '**** Check for exactly four 4s ****
                f = 0
                FOR k = 1 TO 8
                  IF dice(k) = 4 THEN f = f + 1
                NEXT
                IF f = 4 THEN fours = fours + 1

              NEXT
            NEXT
          NEXT
        NEXT
      NEXT
    NEXT
  NEXT
NEXT

PRINT "Total: "; total
PRINT "Fours: "; fours


Gives:
Code:

Total: 1679616
Fours: 43750


Title: Dice problem
Post by: Agamemnus on April 14, 2004, 08:34:37 PM
That formula can't be right...

It comes out to (n-k)!...


Title: Dice problem
Post by: LooseCaboose on April 14, 2004, 09:30:50 PM
Quote from: "Agamemnus"

That formula can't be right...


Opps, typo. The actual formula looks like this:
Code:

nCk =     n!
      ----------
      k!(n - k)!


You can read all about permutations and combinations here (http://mathforum.org/dr.math/faq/faq.comb.perm.html).


Title: Dice problem
Post by: relsoft on April 15, 2004, 09:11:06 AM
Quote from: "1000101"
Now, I mean a bell curve?  Pass grade 10 Algebra and you will understand ;P


Riiiiigghttttt.  Genius boi?

It was never taught to me.  Now before bragging about intelligence here, can you point a link about a bell curve to me?  It may be called a different name in this parts of the world. :*)


Title: Dice problem
Post by: na_th_an on April 15, 2004, 09:31:43 AM
Maybe "Gauss curve" sounds more familiar to you, Rel.


Title: Dice problem
Post by: relsoft on April 15, 2004, 09:43:37 AM
Thank you very much for the info. Unlike our friend who assumes that because its called that way in his country, it's called that way everywhere else. :*)


Title: Dice problem
Post by: 1000101 on April 18, 2004, 10:29:43 PM
Quote from: "relsoft"
Now before bragging about intelligence here, can you point a link about a bell curve to me?


What the hell are you talking about?  I never said anything about intelligence, I was talking about education.

Quote from: "relsoft"
It may be called a different name in this parts of the world. :*)


True enough.  But, I was taught that it's called a bell curve.  It's also known as a normal curve or in statistics, a normal distrobution.  I've never heard it refered to as a 'gauss curve', but after reading a bit on it, it's the same thing.

Quote from: "LooseCaboose"
To illustrate I'll use a situation with 4 dice where we want exactly 2 of the dice to be fours


You'll notice, that the original question didn't say, 'exactly 5 of 8 sides equalling 4', it said, '5 sides of 4 on eight 6 sided dice.'  So, 6 sides of 4 would be two instances of 5 sides of 4.


Title: Dice problem
Post by: LooseCaboose on April 19, 2004, 01:56:22 AM
Quote from: "1000101"

You'll notice, that the original question didn't say, 'exactly 5 of 8 sides equalling 4', it said, '5 sides of 4 on eight 6 sided dice.' So, 6 sides of 4 would be two instances of 5 sides of 4.


Ugh, I did four fours, instead of five fours anyway. Stupid numbers ;-). I interpretted the question as being exactly five fours using eight dice, because the question doesn't specify otherwise.


Title: Dice problem
Post by: relsoft on April 19, 2004, 02:44:21 AM
That's why I was asking you if it was the same as "Skewness".  Skewness is technically a *curve*.


Title: Dice problem
Post by: 1000101 on April 19, 2004, 04:59:12 PM
Yeah, the fact that it didn't state 'exactly' is why I went with the 'multiple sub-sets in the set' method.

As to skewness, I've never heard it refered to as a curve, rather as a linear progressive offset.


Title: Dice problem
Post by: SCM on April 19, 2004, 09:30:49 PM
The problem asks for the number of times you get 5 fours on a roll of 8 dice. As TBBQ said this is different from the number of times you get atleast 5 fours.  It means exactly 5 fours.

Since each of the 1000 rolls is independent, as Rel wrote at the beginning, the expected number is the number of rolls times the probability for one roll.  The probability of getting r fours on n dice has a binomial distribution:
Code:
n = 8          ' The number of dice
r = 5          ' The number of fours
p = 1 / 6      ' The probability of rolling a four on one die
nCr = 56       ' This is n choose r.   Loose told us this is n! / (r! * (n - r)!)
               ' And TBBQ had calculated it earlier

Pof5_4s = nCr * p^r * (1 - p)^(n - r)
The result is a probability of 0.0041676

1000 * 0.0041676 = 4.17
This is the same number that Aga posted (though he rounded it to 4) and close to the experimental values that TBBQ got.

This program compares the results of a probability experiment with the expected values. It runs a number of trials of 1000 rolls, and uses a bar chart to display the results.  Cyan lines on the graph indicate calculated values.  

If you run the program, you will see that some trials will have have results that are way off the calculated mean, but the average of them is usually very close to it.  You will also see that even those results are predicted fairly well.

If you change the number of fours, the program will automatically change the graph window and scale.  It will always get you close to where you want to be, but doesn't work perfectly.  You can set AutoFit = 0 and put in your own settings to correct.

Another of my simple programs that got out of control

You might want to look at my Choose function.  It is more efficient than following the formula literally, and will handle larger numbers because it doesn't multiply everything out.

Code:
DECLARE SUB ScreenSetup (BShft%, HSpc%, Shift%, Range%, Zero%)
DECLARE SUB DataBox (p!, Rolls AS INTEGER)
DECLARE SUB PlotExpectedVals (BShft%, HSpc%, Max%, Min%, n%, p AS SINGLE, Trials%, Zero%)
DECLARE FUNCTION Choose# (n%, r0%)
DEFINT A-Z
DIM Results(500) AS INTEGER

DIM p AS SINGLE
DIM Total AS LONG
DIM Mean AS SINGLE, Median AS SINGLE, Mode AS INTEGER
DIM TotDev AS SINGLE, SDev AS SINGLE

'--------------------- Experiment Parameters ----------------------
 Dice = 8               ' number of dice rolled at a time          '
 Faces = 6              ' number of sides on a die                 '
 Num4s = 5              ' desired number of 4's per roll           '
 Rolls = 1000           ' number of rolls of dice per trial        '
 Trials = 1000          ' total number of trials                   '
'------------------------------------------------------------------'
'------------- Change to fit the graph on the screen --------------
 AutoFit = -1            ' fit graph on screen based on calculations
 HSpc = 4               ' text spaces per bar on the graph (1-5)   '
 Shift = 0              ' minimum value on the graph (0-240)       '
'------------------------------------------------------------------'
p = Choose(Dice, Num4s) * (Faces - 1) ^ (Dice - Num4s) / Faces ^ Dice

Mean = Rolls * p
Median = INT(Mean + .5)
Mode = Median
SDev = SQR(Mean * (1 - p))

IF AutoFit THEN
  HSpc = 76 / (7 * SDev)
  IF HSpc < 1 THEN HSpc = 1
  IF HSpc > 8 THEN HSpc = 8

  Shift = Mean - 3.5 * SDev
  IF Shift < 0 THEN Shift = 0
END IF

Range = 76 \ HSpc - 1
BShft = 32 - 4 * ((HSpc + 1) MOD 2) + 8 * ((76 - HSpc * (Range + 1)) \ 2)
Zero = 444

ScreenSetup BShft, HSpc, Shift, Range, Zero
DataBox p, Rolls
PlotExpectedVals BShft, HSpc, Range + Shift, Shift, Rolls, p, Trials, Zero

RANDOMIZE TIMER
MinResult = Rolls
MaxResult = 0

FOR Trial = 1 TO Trials  '-------- Probability Experiment-----------
  Count = 0                                                          
  FOR roll = 1 TO Rolls                                          
    Fours = 0
    FOR Die = 1 TO Dice
      Face = RND * Faces + .50000001#
      Fours = Fours - (Face = 4)
    NEXT
    Count = Count - (Fours = Num4s)
  NEXT

  IF Count < MinResult THEN MinResult = Count
  IF Count > MaxResult THEN MaxResult = Count
  Results(Count) = Results(Count) + 1
  Total = Total + Count

  IF Count >= Shift AND Count <= Shift + Range THEN
    x0 = 8 * HSpc * (Count - Shift) + BShft
    LINE (x0, Zero - Results(Count))-STEP(8 * HSpc - 2, 0), 14
  END IF
  IF INKEY$ = CHR$(27) THEN GOTO EndProgram
NEXT  '------------- End of Probability Experiment------------------

PlotExpectedVals BShft, HSpc, Range + Shift, Shift, Rolls, p, Trials, Zero

Mean = Total / Trials
Mode = 0
ModeAmount = 0
Median = -1
n = 0

FOR i = MinResult TO MaxResult
  IF Results(i) > ModeAmt THEN
    Mode = i
    ModeAmt = Results(i)
  END IF
  n = n + Results(i)
  IF Median = -1 THEN
    IF n = Trials / 2 THEN
      Median = i + .5
    ELSEIF n > Trials / 2 THEN
      Median = i
    END IF
  END IF
  TotDev = TotDev + Results(i) * (i - Mean) ^ 2
NEXT
SDev = SQR(TotDev / (Trials - 1))

LOCATE 4, 73: PRINT USING "###.###"; Mean
LOCATE 6, 73: PRINT USING "###.#  "; Median
LOCATE 8, 73: PRINT USING "###    "; Mode
LOCATE 10, 73: PRINT USING "###.###"; SDev

EndProgram:
COLOR 8

DEFDBL A-Z
FUNCTION Choose (n%, r0%)
  IF r% > n% THEN
    PRINT "ERROR: n must be greater than or equal to r"
    STOP
  END IF
  IF n% - r0% > r0% THEN
    r% = n% - r0%
  ELSE
    r% = r0%
  END IF
  C = 1
  FOR i% = 1 TO n% - r%
    C = C * (n% + 1 - i%)
    C = C / i%
  NEXT
  Choose = C
END FUNCTION

DEFSNG A-Z
SUB DataBox (p, Rolls AS INTEGER)
  Mean = Rolls * p
  LINE (442, 0)-STEP(200, 88), 0, BF
  LOCATE 2, 64: PRINT "EXPECTED  ACTUAL"
  LOCATE 4, 57: PRINT "MEAN   ";
                PRINT USING "###.###"; Mean
  LOCATE 6, 57: PRINT "MEDIAN ";
                PRINT USING "###.#"; INT(Mean + .5)
  LOCATE 8, 57: PRINT "MODE   ";
                PRINT USING "###"; INT(Mean + .5)
  LOCATE 10, 57: PRINT "ST.DEV.";
                PRINT USING "###.###"; SQR(Mean * (1 - p))
END SUB

DEFINT A-Z
SUB PlotExpectedVals (BShft, HSpc, Max, Min, n, p AS SINGLE, Trials, Zero)
 ' This sub plots the expected number of trials in which the right number
 ' of dice have a face value of 4 exactly r times, for each r.
 ' The expected value has a binomial distribution given by
 '       E = (nCr * p^r * (1-p)^(n-r)) * Trials
 ' where nCr (read n Choose r) = n! /(r! * (n-r)!)  Handled by Choose(n, r)
 '       n is the number of times dice are rolled in a trial
 '       r is the number of times the right number of fours occur
 '       p is the probability of getting the right number of fours in a roll
 '       Trials is the total number of trials to be run

  FOR r = Min TO Max
    Expected = Trials * Choose(n, r) * p ^ r * (1 - p) ^ (n - r)
    x = HSpc * (8 * (r - Min) + 2) + BShft
    IF Expected <> 0 THEN
      LINE (x, Zero - Expected)-STEP(HSpc * 4 - 2, 0), 3
    END IF
  NEXT
END SUB

SUB ScreenSetup (BShft, HSpc, Shift, Range, Zero)
  SCREEN 12
  WIDTH 80, 60

  LINE (26, Zero + 1)-(636, 0), 1, BF                 'Graph box

 '-=-=-=-=-=-=-=-=-=-=-=-=- Make Grid -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  FOR i = 0 TO 440 STEP 20
    IF (i MOD 200) = 0 THEN '------ Write Vertical Labels -------
      LOCATE 56 - 25 * (i \ 200), 1                              '
      PRINT USING "###"; i;                                      '
    END IF    '--------------------------------------------------
    '------------------ Vertical Scale --------------------------
    LINE (25, Zero - i)-STEP(611, 0), 8 + (i MOD 100 = 0)
  NEXT
                                                                     
  Clmn0 = (85 - HSpc * Range) \ 2
  FOR i = 0 + Shift TO Range + Shift '--- Horizontal Labels ---
    IF ((i + 1) MOD 2) OR (HSpc > 1) THEN                        '
                                                                 '
      IF i > 99 THEN ' Print Hundreds - - - - - - - - - - - - -  '
        LOCATE 57, HSpc * (i - Shift) + Clmn0                  ' '
        PRINT USING "#"; i \ 100;                              ' '
      END IF   '- - - - - - - - - - - - - - - - - - - - - - - -' '
                                                                 '
      IF i > 9 THEN  ' Print Tens - - - - - - - - - - - - - - -  '
        LOCATE 58, HSpc * (i - Shift) + Clmn0                  ' '
        PRINT USING "#"; (i MOD 100) \ 10;                     ' '
      END IF   '- - - - - - - - - - - - - - - - - - - - - - - -' '
                                                                 '
      ' Print Ones  - - - - - - - - - - - - - - - - - - - - - -  '
      LOCATE 59, HSpc * (i - Shift) + Clmn0                    ' '
      PRINT USING "#"; i MOD 10;                               ' '
      ' - - - - - - - - - - - - - - - - - - - - - - - - - - - -' '
    END IF                                                       '
  NEXT '---------------------------------------------------------
 
END SUB


Title: Dice problem
Post by: relsoft on April 20, 2004, 07:57:19 AM
Quote from: "1000101"
Yeah, the fact that it didn't state 'exactly' is why I went with the 'multiple sub-sets in the set' method.

As to skewness, I've never heard it refered to as a curve, rather as a linear progressive offset.


Skewness in my book checks for the "skewness" against the normal distribution.

The formula, if remember correctly is:

Code:
3(Mean-Median)
--------------------
     Std


SCM nice code. Saving this....


Title: Dice problem
Post by: Agamemnus on April 20, 2004, 10:16:43 AM
Nice. So it is ~4. :)


Title: Dice problem
Post by: 1000101 on April 21, 2004, 05:05:39 AM
Quote from: "SCM"
The problem asks for the number of times you get 5 fours on a roll of 8 dice. As TBBQ said this is different from the number of times you get atleast 5 fours.  It means exactly 5 fours.


The exclusion of a denomination does not mean the specific inclusion of a NOT denomintation, ie: 5 of 8 != NOT 5 of 8.


Title: Dice problem
Post by: SCM on April 21, 2004, 04:18:00 PM
1000101,

The results are different for the cases of 5 and 5 or more, so we have confusion here.  That is why there are different terms for different situations, just so there will not be any confusion.  There different ways to state each case, but there is no overlap.

r < 5  : "Less than 5", "Fewer than 5"
r <= 5: "At most 5", "5 or less",  "Not more than 5"
r = 5 : "5", "exactly 5"
r >= 5: "Atleast 5", "5 or more", "Not less than 5"
r > 5 : "greater than 5", "More than 5"

If a problem asks for the number of times you get 5 fours, it is asking for exactly 5 fours.  The cases of 6, 7, and 8 fours are excluded, because they were not included.


Title: Dice problem
Post by: 1000101 on April 21, 2004, 06:03:13 PM
As I said, the exclusion of a specifier doesn't mean the NOT inclusion.

Just because '5' was stated, it does *not* mean necessaraly that it is to be 'exactly' 5.  On that note 6, 7 or 8 are valid since 6, 7 and 8 all include the subset of 5.


Title: Dice problem
Post by: Agamemnus on April 22, 2004, 12:22:17 AM
=\