Probability of rolling all 6 dice faces
The probability of rolling every number from 1 to 6 with $N$ six-sided dice.
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
- A probability, $0 \le p \le 1$
- This is the probability of seeing every number from 1 to 6 at least once after rolling $N$ six-sided dice 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 : 0
2 : 0
3 : 0
4 : 0
5 : 0
6 : 0.015432
7 : 0.054012
8 : 0.114026
9 : 0.189043
10 : 0.271812
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.
cQuents, 10 bytes ```text …
1y ago
Dyalog APL, 19 bytes (with …
1y ago
Thunno 2, 14 bytes ``` 6Rẉ …
1y ago
Dyalog APL, 21 bytes ```apl …
1y ago
4 answers
Dyalog APL, 19 bytes
(with index origin zero)
{-/(!∘6×⍵*⍨6÷⍨⊢)⍳7}
This isn't bruteforce! The number of cases where all six faces appear are the OEIS sequence A000920.
\[ P_n = \frac{\mathsf{OEIS}_{\text{A000920}}(n)}{6^n} \]which is
\[ \frac {6!} {6^n} \left\lbrace {n \atop 6}\right\rbrace \]where $\left\lbrace{n\atop k}\right\rbrace$ are the Stirling numbers of the second kind, which expand like this:
\[ \frac 1 {6^n} \sum_{i=0}^6 \left(-1\right)^{6-i} i^n \binom 6 i \](where $\binom n k$ are binomial coefficients) which simplifies to
\[ \sum_{i=0}^6 \left(-1\right)^i \binom 6 i\left(\frac i 6\right)^n \]Note that the $i=0$ term is zero, but it is useful for me because it means the first term of the alternate sum has a positive coefficient.
Code explanation:
-
⍳7
To the list of numbers 0 through 6, apply the following function:-
!∘6
$\binom 6 x$ -
×
times -
6÷⍨⊢
$\frac x 6$ -
⍵*⍨
to the power of $n$
-
-
-/
and then take the alternate sum
Kinda sad to see a bruteforce solution is smaller than the closed form, I'll try and find a language where this is even shorter :)
Side note: this seems to have some floating point error which means the results for $n \in \left\{ 3, 4, 5 \right\}$ aren't exactly zero but in the range of $10^{-16}$ with ⎕fr←647
and $10^{-34}$ with ⎕fr←1287
. either of these are both within the 6 decimal places of precision required, but just in case you cared about the pure computation, I think this is just error stacking up.
2 comment threads
Thunno 2, 14 bytes
6RẉDæḳ6R⁼;,ḷẸ\
Note: brute-force, so times out for $n\ge5$ on ATO, but I have verified it with $n=6$ on my computer.
Explanation
6RẉDæḳ6R⁼;,ḷẸ\ # Implicit input
6R # Push [1..6]
ẉ # Cartesian power with input
D # Duplicate the list
æ ; # Filter by:
ḳ # Sorted uniquify
6R⁼ # Exactly equal to [1..6]?
, # Pair with original list
ḷ # Length of each list
Ẹ # Dump
\ # Swapped divide
# Implicit output
0 comment threads
Dyalog APL, 21 bytes
{≢⍸6=(≢∪)¨⍳⍵/6}÷(6∘*)
Bruteforce solution :)
-
⍳⍵/6
n-dimensional array of all possibilities of rolling $n$ dice, each element is a vector of the dice values -
≢
count the elements -
⍸
where it is true that -
(≢∪)¨
the length of the unique elements -
6=
is equal to 6 -
÷
and divide by -
6∘*
6 to the power of $n$
0 comment threads
cQuents*, 10 bytes
O920A$/6^$
That * there is because, while this should be a specification-correct program that does the correct computation, the only interpreter available does not implement importing any OEIS sequence, but just a few. I suppose this might suggest this is not a valid submission, in which case I would still want to publish it but mark it as non-competing, which I don't think the leaderboard below the question understands as a concept
Anyways, this code does this:
-
O920A$
the input-th item of the OEIS sequence A000920 -
/
divided by -
6^$
6 to the power of the input
0 comment threads