Created
May 26, 2016 02:08
-
-
Save matmoody/e3ef0c94252ef77d665e3a20a5eb9c49 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 random | |
import matplotlib.pyplot as plt | |
# Normally distributed random variable with expected value 0 and variance 1 | |
for _ in range(10): | |
if(random.random() < .5): | |
print "head" | |
else: | |
print "tail" | |
class Coin(object): | |
'''fair coin, can be pseudorandomly flipped''' | |
sides = ('heads', 'tails') | |
last_result = None | |
def flip(self): | |
self.last_result = result = random.choice(self.sides) | |
return result | |
# Manipulate the coins: | |
def create_coins(number): | |
'''create list of number of coin objects''' | |
return [Coin() for _ in xrange(number)] | |
def flip_coins(coins): | |
'''side effect function, modifies object in place, returns None''' | |
for coin in coins: | |
coin.flip() | |
def count_heads(flipped_coins): | |
return sum(coin.last_result == 'heads' for coin in flipped_coins) | |
def count_tails(flipped_coins): | |
return sum(coin.last_result == 'tails' for coin in flipped_coins) | |
def main(): | |
coins = create_coins(1000) | |
for i in xrange(100): | |
flip_coins(coins) | |
print count_heads(coins) | |
if __name__ == '__main__': | |
main() | |
# Normal variable. | |
from numpy.random import normal | |
s = normal(size=(1042*32,)) | |
hist(s, bins=50) | |
for _ in range(10): | |
print np.var(normal(size=(1024*32,))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment