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:

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.

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