Created
February 26, 2022 08:28
-
-
Save joriki/b458c6bc745fd9351dfe1eea89860aa2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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