Created
June 1, 2018 10:44
-
-
Save wederbrand/48e81e7563a2925e67b9478c5f82de7b to your computer and use it in GitHub Desktop.
simple dice test
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 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