Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Comments on Expected value of highest dice rolled

Parent

Expected value of highest dice rolled

+2
−0

You roll $N$ six-sided dice simultaneously. Your score is the highest number rolled. If you play this game many times, what is the expected value (mean) of your score?

Input

  • A positive integer $N$.
  • Your code must work for inputs up to and including 10, but may crash, error, or give incorrect output for larger inputs.

Output

  • The expected value (the mean value) of the highest individual dice result when $N$ six-sided dice (with face values 1, 2, 3, 4, 5, 6) are rolled simultaneously.
  • For inputs up to and including 10, your output is valid if rounding it to 6 decimal places results in the output shown in the test cases.

Note that this means that if you find an incorrect algorithm that happens to give the correct result when rounded to 6 decimal places for inputs from 1 to 10, that is still a valid entry.

Test cases

Test cases are in the format input : output.

1 : 3.500000
2 : 4.472222
3 : 4.958333
4 : 5.244599
5 : 5.430941
6 : 5.560292
7 : 5.654117
8 : 5.724354
9 : 5.778177
10 : 5.820159

Scoring

This is a code golf challenge. Your score is the number of bytes in your code.

Explanations are optional, but I'm more likely to upvote answers that have one.

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Number of faces on a die (4 comments)
Post
+2
−0

Dyalog APL, 14 bytes

{6-+/⍵*⍨6÷⍨⍳5}

Not bruteforce! An exact implementation of the formula

\[ E_n = 6 - \sum_{i=1}^5 \left(\frac i 6\right)^n \]
  • 6- 6 minus
  • +/ the sum of
  • 6÷⍨⍳5 the list 1/6, 2/6, 3/6, 4/6, 5/6
  • ⍵*⍨ to the power of the argument of the function

Formula explanation

For simplicity, let's set $n=2$. This process trivially expands to higher dimensions.

If we roll 2 dice, this table represents all the possible pairs:

      ⍳6 6
┌───┬───┬───┬───┬───┬───┐
│1 1│1 2│1 3│1 4│1 5│1 6│
├───┼───┼───┼───┼───┼───┤
│2 1│2 2│2 3│2 4│2 5│2 6│
├───┼───┼───┼───┼───┼───┤
│3 1│3 2│3 3│3 4│3 5│3 6│
├───┼───┼───┼───┼───┼───┤
│4 1│4 2│4 3│4 4│4 5│4 6│
├───┼───┼───┼───┼───┼───┤
│5 1│5 2│5 3│5 4│5 5│5 6│
├───┼───┼───┼───┼───┼───┤
│6 1│6 2│6 3│6 4│6 5│6 6│
└───┴───┴───┴───┴───┴───┘

Let's take the maximum of each pair:

      ⌈/¨⍳6 6
1 2 3 4 5 6
2 2 3 4 5 6
3 3 3 4 5 6
4 4 4 4 5 6
5 5 5 5 5 6
6 6 6 6 6 6

Notice that if we sum the whole table and divide by $6^n$, we get our desired expected value

      36÷⍨+/,⌈/¨⍳6 6
4.472222222

Alright, so we don't really care about the table, just its sum. I'll print it alongside the table as we build it.

      (P←{⍵(+/,⍵)})⌈/¨⍳6 6
┌───────────┬───┐
│1 2 3 4 5 6│161│
│2 2 3 4 5 6│   │
│3 3 3 4 5 6│   │
│4 4 4 4 5 6│   │
│5 5 5 5 5 6│   │
│6 6 6 6 6 6│   │
└───────────┴───┘

Let's find another way to build the same table (sum). Start with a table of all 6's.

P 6 6⍴6
┌───────────┬───┐
│6 6 6 6 6 6│216│
│6 6 6 6 6 6│   │
│6 6 6 6 6 6│   │
│6 6 6 6 6 6│   │
│6 6 6 6 6 6│   │
│6 6 6 6 6 6│   │
└───────────┴───┘

We want to subtract 1 from all entries except for those in the last row or column. Alternatively, we want to subtract this table from ours:

      ~6∊¨⌈/¨⍳6 6
1 1 1 1 1 0
1 1 1 1 1 0
1 1 1 1 1 0
1 1 1 1 1 0
1 1 1 1 1 0
0 0 0 0 0 0

This is a $5\times 5$ square.

We now get the following table:

      P (6 6⍴6)-(~6∊¨⌈/¨⍳6 6)
┌───────────┬───┐
│5 5 5 5 5 6│191│
│5 5 5 5 5 6│   │
│5 5 5 5 5 6│   │
│5 5 5 5 5 6│   │
│5 5 5 5 5 6│   │
│6 6 6 6 6 6│   │
└───────────┴───┘

Repeating the same process for squares of side 4, 3, 2 and 1, we get exactly the original table (and sum!) we wanted.

Expressed mathematically, the expected value can therefore be written as

\[ \frac 1 {6^n} \left(6\cdot6^n - 5^n - 4^n - 3^n - 2^n - 1^n\right) = 6 - \sum_{i=1}^5 \left(\frac i 6\right)^n \]
History
Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

Impressive formula (8 comments)
Impressive formula
trichoplax‭ wrote 10 months ago

I had to implement this formula and test it for myself because it looks so different from the formula I used to generate the test cases that I couldn't imagine it giving the same result - but it does!

For comparison, here's the formula I used:

$$\frac{1}{6^N}\sum_{i=1}^6 i(i^N-(i-1)^N)$$

Much less golfy...

trichoplax‭ wrote 10 months ago · edited 10 months ago

I was curious so I tried to derive your formula from mine. Managed it by unrolling the sum and then rolling it back up at the end. No idea if this is similar to how you found it.

$$\frac{1}{6^N}\sum_{i=1}^6 i(i^N-(i-1)^N)$$

$$\frac{1}{6^N}\sum_{i=1}^6 i(i)^N-i(i-1)^N$$

$$\frac{1}{6^N}\left(\displaylines{1(1)^{N}-1(0)^N \\+ 2(2)^{N}-2(1)^N \\+ 3(3)^{N}-3(2)^N \\+ 4(4)^{N}-4(3)^N \\+ 5(5)^{N}-5(4)^N \\+ 6(6)^{N}-6(5)^N}\right)$$

$$\frac{1}{6^N}\left(\displaylines{1(1)^{N}-2(1)^N \\+ 2(2)^{N}-3(2)^N \\+ 3(3)^{N}-4(3)^N \\+ 4(4)^{N}-5(4)^N \\+ 5(5)^{N}-6(5)^N \\+ 6(6)^{N}}\right)$$

$$\frac{1}{6^N}\left(\displaylines{(1-2)1^N \\+ (2-3)2^N \\+ (3-4)3^N \\+ (4-5)4^N \\+ (5-6)5^N \\+ (6)6^{N}}\right)$$

$$\frac{1}{6^N}\left(\displaylines{-1^N \\-2^N \\-3^N \\-4^N \\-5^N \\-6^{N}+ (7)6^{N}}\right)$$

$$\frac{1}{6^N}\left(7(6)^{N}-\sum_{i=1}^6 i^{N}\right)$$

$$7-\sum_{i=1}^6 \left(\frac{i}{6}\right)^{N}$$

RubenVerg‭ wrote 10 months ago

I found it by first writing just the sum, without the 7 - part, realized it was accidentally decreasing in the limit but that the n=1 case was correct, so I just tried to flip it and shift it up and it worked. I'm not really sure how it works - still trying to figure that out :) thanks for working out the connection with your solution, maybe that will help me understand why it is correct combinatorially, I'll try to remember to write a better explanation in the post once I do.

trichoplax‭ wrote 10 months ago

Even though I've convinced myself they are equivalent, I can't visualise yours, whereas I can understand mine geometrically so it makes sense to me intuitively.

If you want the geometric interpretation of mine, $i^N-(i-1)^N$ is the number of ways of rolling $N$ dice that lead to $i$ being the highest result.

When $N=2$ the possible results can be thought of as a 6 by 6 square grid, and labelling each square with its highest result gives L shapes which are the difference between a square and a square 1 smaller.

When $N=3$ the possible results can be thought of as a 6 by 6 by 6 cubic lattice, and labelling each cube with its highest result gives shells one cube thick which are the difference between a cube and a cube 1 smaller.

In any number of dimensions (any value of $N$), the regions that have the same highest result are at the same Chebyshev distance from $(1, 1, 1)$.

RubenVerg‭ wrote 10 months ago

Yep, I read your sol and instantly went "oh this is a generalization of the 2n-1 "shells" for the 6x6 table. Still trying to figure out mine :) and also why the one I had without 7- doesn't give the correct answer (like, I get why it can't work - it's decreasing instead of approaching 6 in the limit, but I don't get why it's wrong)

RubenVerg‭ wrote 10 months ago

trichoplax‭ I've added an explanation for what I visualize my function to be doing (and also realized that, of course, $7 - \left(\frac 6 6\right)^n$ is just $6$ :) )

trichoplax‭ wrote 10 months ago

That's a really intuitive explanation. I don't know APL and I still found the visualisation easy to follow with the sequence of tables.

RubenVerg‭ wrote 10 months ago

thanks for the feedback, was experimenting with this new style for long visual explanations - I would've generated the tables with APL anyways, might as well put the code in as well :)