little math/programming problems April 2023
|
04-28-2023, 11:55 AM
Post: #1
|
|||
|
|||
little math/programming problems April 2023
I have a problem someone could probably simulate faster than I can work it out. I have a set of dice with values: 0, 0, 0, 0, 2, 3. You roll the dice, add up the total, then roll that number of dice. And repeat. If I start with five dice, how long will the game last?
Source: https://twitter.com/jamesgrime/status/16...46721?s=20 (contains solutions that spoil the challenge a bit) Wikis are great, Contribute :) |
|||
04-28-2023, 10:16 PM
Post: #2
|
|||
|
|||
RE: little math/programming problems April 2023
Simulating the experiment 100,000 times, I get an average of 5.49893 rolls with 5 dice. Here are the results with different numbers of starting dice:
Code:
Dave |
|||
04-29-2023, 06:50 PM
Post: #3
|
|||
|
|||
RE: little math/programming problems April 2023
Maybe possible with the generating function and some ugly recursions?
\( (z^3 + z^2 + 4)^n \) where \(n \) is the number of dice. \( f(5) = z^{15} + 5z^{14} + 10z^{13} + 30z^{12} + 85z^{11} + 121z^{10} + 240z^9 + 500z^8 + 480z^7 + 800z^6 + 1280z^5 + 640z^4 + 1280z^3 + 1280z^2 + 1024 \) (Interesting it's not possible to roll a sum equal to 1 no matter how many dice you use!) Since the number of dice changes based on the previous roll, you'd need to recursively add up the probabilities for every possible branch in order to calculate an (more?) exact answer - multiplying the conditional probability of each branch times the total chances of arriving at that. Using the initial state of \( f(5) \) and creating a markov chain 24 layers deep you can cover 99% of the expected cases. Among 10M games with 5 initial dice I got this estimation:
I also tried with 1000 to 1M dice at a time.. the average number of rolls asymptotically approaches something around \( 4.32 * ln(n) \) with 1000-dice games ending in 32 rolls on average. 17bii | 32s | 32sii | 41c | 41cv | 41cx | 42s | 48g | 48g+ | 48gx | 50g | 30b |
|||
04-30-2023, 01:32 AM
(This post was last modified: 04-30-2023 01:41 AM by Allen.)
Post: #4
|
|||
|
|||
RE: little math/programming problems April 2023
I believe the first few terms are now calculated (hopefully correct). Starting with 5 dice, the chances of the game terminating on exactly x rolls is:
Code:
The total number of states for depth grows quickly to [1, 15, 375, 17655, 1469850, , ...] After that things get out of hand Here's the difference between the Monte Carlo sampling and the exact calculation: Code:
10M rolls Total Expected Value 5.4931065 Code:
17bii | 32s | 32sii | 41c | 41cv | 41cx | 42s | 48g | 48g+ | 48gx | 50g | 30b |
|||
05-02-2023, 08:03 PM
Post: #5
|
|||
|
|||
RE: little math/programming problems April 2023
(04-29-2023 06:50 PM)Allen Wrote:I'm surprised that we came up with such different answers. With 10 million trials, I got an average of 5.49603. Here's my C++ code. Does anyone spot a problem? Maybe this is a case of rand() showing it's limitations? Code: #include<iostream> //Required for cin, cout $ ./foo Enter starting number of dice and number of simulations: 5 10000000 Average of 5.49603 rolls starting with 5 dice |
|||
05-03-2023, 07:10 AM
(This post was last modified: 05-03-2023 07:11 AM by ThomasF.)
Post: #6
|
|||
|
|||
RE: little math/programming problems April 2023
(05-02-2023 08:03 PM)David Hayden Wrote: Here's my C++ code. Does anyone spot a problem? Maybe this is a case of rand() showing it's limitations? Hi David! I assume this is due to the limitations of rand(). With different seeds I get the following result (ie your code but added seed initialization). Code: srand (time(NULL)); Code: Average of 5.48456 rolls starting with 5 dice Using another random generator (PCG), I get the following result: Code: Average of 5.49419 rolls starting with 5 dice Edit: All runs used 5 dice and 10M simulations. Cheers, Thomas [35/45/55/65/67/97/80 21/25/29C 31E/32E/33E|C/34C/38E 41C|CV|CX 71B 10C/11C/12C/15C|CE/16C 32S|SII/42S 28C|S 48GX/49G/50G 35S 41X] |
|||
05-03-2023, 12:18 PM
(This post was last modified: 05-04-2023 12:13 PM by Albert Chan.)
Post: #7
|
|||
|
|||
RE: little math/programming problems April 2023
(05-02-2023 08:03 PM)David Hayden Wrote: Does anyone spot a problem? Maybe this is a case of rand() showing it's limitations? rand(), and many other random number generators, have weaker random low bits. We should use random high bits instead. < int side = sides[rand() % 6]; > int side = sides[rand() * 6ull / (RANDMAX+1u)]; Bonus: (RAND_MAX+1u) is likely pow-of-2, division should compiled as fast bits shift. Note: we can speed up a bit more, replacing 6ull by 6u, if it does not cause overflow. I have ignored slight bias mapping rand() to 0 .. 5 see http://www.azillionmonkeys.com/qed/random.html see https://lemire.me/blog/2019/06/06/nearly...s-systems/ However, because of long tails, it is hard to get good estimate, even with good random numbers. For 5 dices, correct expected value = 5.491 819 ... |
|||
05-12-2023, 06:14 PM
Post: #8
|
|||
|
|||
RE: little math/programming problems April 2023
(05-02-2023 08:03 PM)David Hayden Wrote: int sides[6] = {0,0,0,0,2,3}; For a rough idea of expected values, we may try a simple "random" generator. 5 dices, with cycled pattern, different initial state: Code: 000023000023000023000023000023000023 ... Pattern Note that 6 throws add to sum of only 5, sequence will always terminate. Expected values = (3 + 4 + 5 + 6 + 7 + 6) / 6 = 31/6 ≈ 5.2 This "random" generator is super lumpy, with maximal 4 0's in a row. We expected true expected values slightly bigger. |
|||
05-14-2023, 12:16 PM
Post: #9
|
|||
|
|||
RE: little math/programming problems April 2023
The following pRNG may be helpful if sampling the dice throws is a problem. It's a specially constructed linear congruential generator designed for die rolling.
Letting X stand for the internal state, one has X = X * 93847949523997033 Mod ( 216172782113784439 ) The die roll is X ( Mod 6 ) or X ( Mod 6 ) + 1 The only constraint is that the first X must be non-zero 0 < Xo < 93847949523997033. If this version has distributional problems, I got millions more available. |
|||
05-15-2023, 09:26 PM
(This post was last modified: 05-15-2023 11:09 PM by Gilles.)
Post: #10
|
|||
|
|||
RE: little math/programming problems April 2023
My (New)RPL version :
Code: « → Loop Execution on my laptop and HP50g, NewRPL Code: Loops Time(s) Result Time |
|||
05-16-2023, 07:47 PM
Post: #11
|
|||
|
|||
RE: little math/programming problems April 2023
Finally a calculator version as well, thanks Gilles! (and all the others as well of course)
Admittedly I could produce a calculator version too, but I didn't prioritize the task. Wikis are great, Contribute :) |
|||
05-16-2023, 09:27 PM
(This post was last modified: 05-16-2023 09:30 PM by Ajaja.)
Post: #12
|
|||
|
|||
RE: little math/programming problems April 2023
My HP-42s/Free42 version:
Code: 00 { 61-Byte Prgm } 100000: 5,49717 1000000: 5,492177 10000000: 5,4922995 |
|||
05-17-2023, 09:48 PM
Post: #13
|
|||
|
|||
RE: little math/programming problems April 2023
Interesting the difference between the newRPL and the Free42.
I thought that the math library (at least for basic stuff like RAND) of the HP42 and RPL was similar but likely it is not. Wikis are great, Contribute :) |
|||
05-18-2023, 07:54 AM
(This post was last modified: 05-18-2023 07:58 AM by Ajaja.)
Post: #14
|
|||
|
|||
RE: little math/programming problems April 2023
I think it just has a large deviation.
I've changed the program a bit to calculate SDEV: Code: 00 { 57-Byte Prgm } So, standard error of the mean here (SDEV/SQRT(N)): 10000: ~0.05 100000: ~0.016 1000000: ~0.005 10000000: ~0.0016 |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 5 Guest(s)