Skip to content

Instantly share code, notes, and snippets.

@wederbrand
Created June 1, 2018 10:44
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 wederbrand/48e81e7563a2925e67b9478c5f82de7b to your computer and use it in GitHub Desktop.
Save wederbrand/48e81e7563a2925e67b9478c5f82de7b to your computer and use it in GitHub Desktop.
simple dice test
import java.util.Random;
public class DiceTest {
public static void main(String[] args) {
Random random = new Random();
int A = 0;
int B = 0;
for (int i = 0; i < 100000; i++) {
switch (rollUntilWinner(random)) {
case "A":
A++;
break;
case "B":
B++;
break;
}
}
System.out.println("A: " + A);
System.out.println("B: " + B);
}
private static String rollUntilWinner(Random random) {
boolean lastWasSeven = false;
while (true) {
int first = random.nextInt(6) + 1;
int second = random.nextInt(6) + 1;
if (first == 6 && second == 6) {
return "A";
}
else if (first + second == 7) {
if (lastWasSeven) {
return "B";
}
lastWasSeven = true;
}
else {
lastWasSeven = false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment