Created
December 27, 2017 15:01
-
-
Save farooqkz/104a7c6600db8368753ca4636cefb67d 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
#!/usr/bin/python3.5 | |
# Hi, I am a program who select a random element from S n times. | |
# you should specify n via m234.py n | |
# My father is Farooq KZ | |
# You can contact him at farooghkarimizadeh@gmail.com | |
# Also I'm under MIT/X11 licence | |
import os # I want it for os.urandom | |
import sys # I want it for sys.argv and sys.exit | |
def get_random(high): | |
""" | |
This function returns a random integer between 0 and high-1 | |
""" | |
random_bytes = os.urandom(int(high.bit_length()/8) + 1) | |
random_int = int(random_bytes.hex(), 16) | |
return random_int % high | |
if len(sys.argv) != 2 or "-h" in sys.argv: | |
print("Usage:", sys.argv[0],"n") | |
sys.exit() | |
n = int(sys.argv[1]) # How many times whould I test? | |
S = [("r", "g")] * 8 + [("r", "r")] * 2 # S like Sample Space! | |
# Also r like red and g like green | |
while [] in S: | |
S.remove([]) | |
print("S =",S) | |
r = 0 # How many times a card was chosen which both of its side was Red? | |
g = 0 | |
for _ in range(n): | |
# Choose a card | |
c = S[get_random(len(S))] | |
# the other side is red or green? | |
if c[1] == "r": | |
r += 1 | |
elif c[1] == "g": | |
g += 1 | |
print("Total", n, "tries. Red:", r,"Green:",g) | |
print("% of Green:", (g/n)*100) | |
print("% of Red:", (r/n)*100) |
Author
farooqkz
commented
Dec 27, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment