Created
October 16, 2015 22:48
-
-
Save allenanie/60e5dac9f38a70803dbb to your computer and use it in GitHub Desktop.
Simulate a coin toss (for Stanford EE178)
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
# simulate coin toss | |
import numpy as np | |
X = np.zeros(3) # random variable X: X[0], X[1], X[2] | |
X_0_sets = set() | |
X_1_sets = set() | |
X_2_sets = set() | |
def evaluate_coins(fivecoins): | |
""" | |
Evaluate one trial's result | |
""" | |
counter = 0 # counts for total occurance | |
for i in range(0, len(fivecoins) - 1): | |
# get the current pos, and the next | |
# check if they satisfy our requirement | |
if i == len(fivecoins) - 1: | |
break | |
if fivecoins[i] == 1 and fivecoins[i + 1] == 0: | |
counter += 1 | |
X[counter] = X[counter] + 1 | |
if counter == 1: | |
X_1_sets.add(str(fivecoins)) | |
elif counter == 2: | |
X_2_sets.add(str(fivecoins)) | |
elif counter == 0: | |
X_0_sets.add(str(fivecoins)) | |
def print_set(name, sets): | |
print(name + " set has length: ", len(sets)) | |
print(sets) | |
if __name__ == '__main__': | |
# run 1000 times | |
for x in range(1, 1000): | |
# every time we toss 5 coins | |
fivecoins = np.random.randint(0, 2, size=5) | |
evaluate_coins(fivecoins) | |
print(X) | |
print_set("X_0", X_0_sets) | |
print_set("X_1", X_1_sets) | |
print_set("X_2", X_2_sets) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment