0% found this document useful (0 votes)
37 views28 pages

Intreoduction To Python Randon Numbers

The document discusses random walks and how to simulate them using Python. It shows how to generate random coin tosses and track the resulting random walk over multiple steps. Increasing the number of simulated random walks produces histograms that approach a normal distribution.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views28 pages

Intreoduction To Python Randon Numbers

The document discusses random walks and how to simulate them using Python. It shows how to generate random coin tosses and track the resulting random walk over multiple steps. Increasing the number of simulated random walks produces histograms that approach a normal distribution.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Random Numbers

I N TE R M E D I ATE P Y TH O N
Can ' t go below step 0
0.1 % chance of falling down
the stairs

Bet: you'll reach step 60


How to solve?
Analytical
Simulate the process
Hacker statistics!
Random generators
import numpy as np
np.random.rand() # Pseudo-random numbers

0.9535543896720104 # Mathematical formula

np.random.seed(123) # Starting from a seed


np.random.rand()

0.6964691855978616

np.random.rand()

0.28613933495037946
Random generators
np.random.seed(123)
np.random.rand()

0.696469185597861 # Same seed: same random numbers!

np.random.rand() # Ensures "reproducibility"

0.28613933495037946
Coin toss
game.py

import numpy as np
np.random.seed(123)
coin = np.random.randint(0,2) # Randomly generate 0 or 1
print(coin)

0
Coin toss
game.py

import numpy as np
np.random.seed(123)
coin = np.random.randint(0,2) # Randomly generate 0 or 1
print(coin)
if coin == 0:
print("heads")
else:
print("tails")

0
heads
Random Walk
I N TE R M E D I ATE P Y TH O N
Random Step
Random Walk
Known in Science

Path of molecules

Gambler's nancial status


Heads or Tails
headtails.py

import numpy as np
np.random.seed(123)
outcomes = []
for x in range(10) :
coin = np.random.randint(0, 2)
if coin == 0 :
outcomes.append("heads")
else :
outcomes.append("tails")
print(outcomes)

['heads', 'tails', 'heads', 'heads', 'heads',


'heads', 'heads', 'tails', 'tails', 'heads']
Heads or Tails: Random Walk
headtailsrw.py

import numpy as np
np.random.seed(123)
tails = [0]
for x in range(10) :
coin = np.random.randint(0, 2)
tails.append(tails[x] + coin)

print(tails)

[0, 0, 1, 1, 1, 1, 1, 1, 2, 3, 3]
Step to Walk
outcomes

['heads', 'tails', 'heads', 'heads', 'heads',


'heads', 'heads', 'tails', 'tails', 'heads']

tails

[0, 0, 1, 1, 1, 1, 1, 1, 2, 3, 3]
Distribution
I N TE R M E D I ATE P Y TH O N
Distribution
Random Walk
headtailsrw.py

import numpy as np
np.random.seed(123)
tails = [0]
for x in range(10) :
coin = np.random.randint(0, 2)
tails.append(tails[x] + coin)
100 runs
distribution.py

import numpy as np
np.random.seed(123)
final_tails = []
for x in range(100) :
tails = [0]
for x in range(10) :
coin = np.random.randint(0, 2)
tails.append(tails[x] + coin)
final_tails.append(tails[-1])
print(final_tails)

[3, 6, 4, 5, 4, 5, 3, 5, 4, 6, 6, 8, 6, 4, 7, 5, 7, 4, 3, 3, ..., 4]
Histogram, 100 runs
distribution.py

import numpy as np
import matplotlib.pyplot as plt
np.random.seed(123)
final_tails = []
for x in range(100) :
tails = [0]
for x in range(10) :
coin = np.random.randint(0, 2)
tails.append(tails[x] + coin)
final_tails.append(tails[-1])
plt.hist(final_tails, bins = 10)
plt.show()
Histogram, 100 runs
Histogram, 1,000 runs
distribution.py

import numpy as np
import matplotlib.pyplot as plt
np.random.seed(123)
final_tails = []
for x in range(1000) : # <--
tails = [0]
for x in range(10) :
coin = np.random.randint(0, 2)
tails.append(tails[x] + coin)
final_tails.append(tails[-1])
plt.hist(final_tails, bins = 10)
plt.show()
Histogram, 1,000 runs
Histogram, 10,000 runs
distribution.py

import numpy as np
import matplotlib.pyplot as plt
np.random.seed(123)
final_tails = []
for x in range(10000) : # <--
tails = [0]
for x in range(10) :
coin = np.random.randint(0, 2)
tails.append(tails[x] + coin)
final_tails.append(tails[-1])
plt.hist(final_tails, bins = 10)
plt.show()
Histogram, 10,000 runs

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy