Qbasicnews.com
April 05, 2020, 01:18:36 AM
 Welcome, Guest. Please login or register. 1 Hour 1 Day 1 Week 1 Month Forever Login with username, password and session length
 Home Help Search Login Register
 Pages: 1 2 [3]
 Author Topic: Dice problem  (Read 9077 times)
LooseCaboose
I hold this place together

Posts: 981

 « Reply #30 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.
 Logged

esus saves.... Passes to Moses, shoots, he scores!
relsoft
*/-\*

Posts: 3927

 « Reply #31 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. :*)
 Logged

y smiley is 24 bit.

Genso's Junkyard:
http://rel.betterwebber.com/
na_th_an
*/-\*

Posts: 8244

 « Reply #32 on: April 15, 2004, 09:31:43 AM »

Maybe "Gauss curve" sounds more familiar to you, Rel.
 Logged

SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
relsoft
*/-\*

Posts: 3927

 « Reply #33 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. :*)
 Logged

y smiley is 24 bit.

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

Posts: 519

 « Reply #34 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.
 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.'
LooseCaboose
I hold this place together

Posts: 981

 « Reply #35 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.
 Logged

esus saves.... Passes to Moses, shoots, he scores!
relsoft
*/-\*

Posts: 3927

 « Reply #36 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*.
 Logged

y smiley is 24 bit.

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

Posts: 519

 « Reply #37 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.
 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.'
SCM
Wandering Guru

Posts: 311

 « Reply #38 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
 Logged

hrist Jesus came into the world to save sinners, of whom I am first.(I Timothy 1:15)

For God so loved the world, that He gave His only begotten Son,
that whoever believes in Him should not perish, but have eternal life.(John 3:16)
relsoft
*/-\*

Posts: 3927

 « Reply #39 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....
 Logged

y smiley is 24 bit.

Genso's Junkyard:
http://rel.betterwebber.com/
Agamemnus
x/ \z

Posts: 3491

 « Reply #40 on: April 20, 2004, 10:16:43 AM »

Nice. So it is ~4.
 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.
1000101
Ancient Guru

Posts: 519

 « Reply #41 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.
 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.'
SCM
Wandering Guru

Posts: 311

 « Reply #42 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.
 Logged

hrist Jesus came into the world to save sinners, of whom I am first.(I Timothy 1:15)

For God so loved the world, that He gave His only begotten Son,
that whoever believes in Him should not perish, but have eternal life.(John 3:16)
1000101
Ancient Guru

Posts: 519

 « Reply #43 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.
 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 #44 on: April 22, 2004, 12:22:17 AM »

=\
 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]
Jump to: