import numpy as np import matplotlib.pyplot as plt %matplotlib inline a = np.random.random_integers(1, 6, 4000) a allprobs = [] for i in range(300): a = np.random.random_integers(1, 6, 4000) allprobs.append(len([x for x in a.reshape(-1, 4) if 6 in x]) / 1000.0) plt.hist(allprobs) trials = 500 overall = [] for t in range(trials): initial = [20] time = 200 for i in range(time): if np.random.random() > 0.5: initial.append(initial[-1] + 1) else: initial.append(initial[-1] - 1) if initial[-1] == 0: break if len(initial) < 201: initial += [0] * (201 - len(initial)) overall.append(initial) b = np.linspace(0, 200, 201) for o in overall: plt.plot(b, o) ends = [] for o in overall: ends.append(o[-1]) plt.hist(ends, 66) import math xsgood = [] ysgood = [] xsbad = [] ysbad = [] good = 0 trials = 5000 for i in range(trials): x = np.random.random() y = np.random.random() if (math.sqrt(x ** 2 + y ** 2) <= 1): good += 1 xsgood.append(x) ysgood.append(y) else: xsbad.append(x) ysbad.append(y) print(4 * float(good) / trials) fig = plt.gcf() ax = fig.add_subplot(111, aspect='equal') plt.plot(xsgood, ysgood, "o") plt.plot(xsbad, ysbad, "o") endx = [] endy = [] trials = 10000 for i in range(trials): x = 0 y = 0 steps = 1000 for i in range(steps): r = np.random.random_integers(1, 4) if r == 1: x += 1 elif r == 2: x -= 1 elif r == 3: y += 1 elif r == 4: y -= 1 endx.append(x) endy.append(y) len(endx), len(endy) plt.scatter(endx, endy) x = [0] y = [0] steps = 10000 for i in range(steps): r = np.random.random_integers(1, 4) if r == 1: x.append(x[-1] + 1) y.append(y[-1]) elif r == 2: x.append(x[-1] - 1) y.append(y[-1]) elif r == 3: x.append(x[-1]) y.append(y[-1] + 1) elif r == 4: x.append(x[-1]) y.append(y[-1] - 1) plt.plot(x, y) plt.plot(0, 0, "go") plt.plot(x[-1], y[-1], "ro")