Skip to content

Instantly share code, notes, and snippets.

@joriki
Created February 26, 2022 08:28
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/b458c6bc745fd9351dfe1eea89860aa2 to your computer and use it in GitHub Desktop.
Save joriki/b458c6bc745fd9351dfe1eea89860aa2 to your computer and use it in GitHub Desktop.
import java.util.Random;
public class Question4390194 {
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 [] 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 = 0;
outer:
for (;;) {
int roll = random.nextInt(6);
nrolls++;
seen |= 1 << roll;
for (int b : bits)
if ((seen & b) == 0)
continue outer;
break;
}
}
System.out.println(nrolls / (double) ntrials);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment