Skip to content

Instantly share code, notes, and snippets.

@joriki
Created May 14, 2020 19:17
Show Gist options
  • Save joriki/2ea9d37625f2999129fb9f55e4ea5f03 to your computer and use it in GitHub Desktop.
Save joriki/2ea9d37625f2999129fb9f55e4ea5f03 to your computer and use it in GitHub Desktop.
Simulate how long it takes until six dice that retain a 1 for 4 turns all show a 1; see https://math.stackexchange.com/questions/3674864.
import java.util.Random;
public class Question3674864 {
final static long ntrials = 10000000;
final static Random random = new Random();
public static void main (String [] args) {
long count = 0;
for (long trial = 0;trial < ntrials;trial++) {
int [] state = new int [6];
int nones;
do {
count++;
nones = 0;
for (int i = 0;i < 6;i++) {
if (state [i] > 0)
state [i]--;
if (state [i] == 0 && random.nextInt(6) == 0)
state [i] = 4;
if (state [i] > 0)
nones++;
}
} while (nones != 6);
}
System.out.println(count / (double) ntrials);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment