Skip to content

Instantly share code, notes, and snippets.

@hengyunabc
Created February 17, 2016 16:50
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 hengyunabc/b9b17753159b45d6a1e9 to your computer and use it in GitHub Desktop.
Save hengyunabc/b9b17753159b45d6a1e9 to your computer and use it in GitHub Desktop.
import java.security.SecureRandom;
public class PenneyGame {
static SecureRandom random = new SecureRandom();
public static void main(String[] args) {
int count = 100;
int winCount = 0;
for (int i = 0; i < 100; ++i) {
if (play()) {
winCount++;
}
}
System.out.println("count:" + count + ", winCount:" + winCount);
}
public static boolean play() {
String result = "";
while (true) {
if (random.nextBoolean()) {
result = result + "1";
} else {
result = result + "0";
}
if (result.contains("011")) {
System.out.println(result);
return true;
}
if (result.contains("110")) {
System.out.println(result);
return false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment