Skip to content

Instantly share code, notes, and snippets.

@joriki
Created February 26, 2022 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joriki/be220f8de517c61fcb445c47c13c2164 to your computer and use it in GitHub Desktop.
Save joriki/be220f8de517c61fcb445c47c13c2164 to your computer and use it in GitHub Desktop.
import java.util.Random;
public class Question4390194b {
final static Random random = new Random();
final static int ntrials = 100000000;
final static int [] [] families = {
{2,4,6},
{2,3,5},
{1,4},
};
final static int [] [] permutations = {
{0,1,2},
{0,2,1},
{1,0,2},
{1,2,0},
{2,0,1},
{2,1,0},
};
final static int [] bits = new int [families.length];
static {
for (int i = 0;i < families.length;i++)
for (int roll : families [i])
bits [i] |= 1 << (roll - 1);
}
public static void main(String [] args) {
long nrolls = 0;
for (int n = 0;n < ntrials;n++) {
int [] seen = new int [6];
outer:
for (;;) {
int roll = 1 << random.nextInt(6);
nrolls++;
for (int p = 0;p < 6;p++)
if ((bits [permutations [p] [seen [p]]] & roll) != 0)
if (++seen [p] == 3)
break outer;
}
}
System.out.println(nrolls / (double) ntrials);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment