User Tools

Site Tools


slush:codetalkers

This is an old revision of the document!


flip.py

import random
total = 0
for i in range(2000000000000000000):
    flip = random.randint(0, 1)
    total = total + flip
    average = total / (i + 1) 
    print(i, flip, total, average)
    

pi.py

import random
def main():
    N_sq = 0
    N_circ = 0
    n_iterations = 10000000000
    for i in range (n_iterations):
        x = random.random() * 2 - 1
        y = random.random() * 2 - 1
        N_sq = N_sq + 1 #Always in square
        if x*x + y*y < 1:
            N_circ = N_circ + 1
        pi_estimate = 4.0 * float(N_circ) / float(N_sq)
        if i % (n_iterations/10000000000) == 0:
            print('%d    %f' % (i, pi_estimate))
main()

walk.py

import random
import math
x = 0
y = 0
for i in range(20000):
    flipx = random.randint(-1, 1)
    flipy = random.randint(-1, 1)
    x = x + flipx
    y = y + flipy  
    r = math.sqrt(x*x + y*y)
    print(i, x, y, r)

General Instructions (Taken from Comments Section on old Code Talkers Classroom)

slush/codetalkers.1495203704.txt.gz ยท Last modified: 2018/11/25 01:33 (external edit)