Created
February 17, 2016 16:50
-
-
Save hengyunabc/b9b17753159b45d6a1e9 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.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