SlideShare a Scribd company logo
Random Numbers
INTERMEDIATE PYTHON
Hugo Bowne-Anderson
Data Scientist at DataCamp
INTERMEDIATE PYTHON
INTERMEDIATE PYTHON
INTERMEDIATE PYTHON
INTERMEDIATE PYTHON
INTERMEDIATE PYTHON
INTERMEDIATE PYTHON
Can't go below step 0
0.1 % chance of falling down
the stairs
Bet: you'll reach step 60
INTERMEDIATE PYTHON
How to solve?
Analytical
Simulate the process
Hacker statistics!
INTERMEDIATE PYTHON
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
INTERMEDIATE PYTHON
Random generators
np.random.seed(123)
np.random.rand()
0.696469185597861 # Same seed: same random numbers!
np.random.rand() # Ensures "reproducibility"
0.28613933495037946
INTERMEDIATE PYTHON
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
INTERMEDIATE PYTHON
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
Let's practice!
INTERMEDIATE PYTHON
Random Walk
INTERMEDIATE PYTHON
Hugo Bowne-Anderson
Data Scientist at DataCamp
INTERMEDIATE PYTHON
Random Step
INTERMEDIATE PYTHON
Random Walk
Known in Science
Path of molecules
Gambler's nancial status
INTERMEDIATE PYTHON
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']
INTERMEDIATE PYTHON
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]
INTERMEDIATE PYTHON
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]
Let's practice!
INTERMEDIATE PYTHON
Distribution
INTERMEDIATE PYTHON
Hugo Bowne-Anderson
Data Scientist at DataCamp
INTERMEDIATE PYTHON
Distribution
INTERMEDIATE PYTHON
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)
INTERMEDIATE PYTHON
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]
INTERMEDIATE PYTHON
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)
plot.show()
INTERMEDIATE PYTHON
Histogram, 100 runs
INTERMEDIATE PYTHON
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)
plot.show()
INTERMEDIATE PYTHON
Histogram, 1,000 runs
INTERMEDIATE PYTHON
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)
plot.show()
INTERMEDIATE PYTHON
Histogram, 10,000 runs
Let's practice!
INTERMEDIATE PYTHON

More Related Content

Similar to chapter5.pdf

error 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docx
error 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docxerror 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docx
error 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docx
SALU18
 
Python
PythonPython
PyCon2009_AI_Alt
PyCon2009_AI_AltPyCon2009_AI_Alt
PyCon2009_AI_Alt
Hiroshi Ono
 
Kotlin for Android Developers - 2
Kotlin for Android Developers - 2Kotlin for Android Developers - 2
Kotlin for Android Developers - 2
Mohamed Nabil, MSc.
 
The Easy-Peasy-Lemon-Squeezy, Statically-Typed, Purely Functional Programming...
The Easy-Peasy-Lemon-Squeezy, Statically-Typed, Purely Functional Programming...The Easy-Peasy-Lemon-Squeezy, Statically-Typed, Purely Functional Programming...
The Easy-Peasy-Lemon-Squeezy, Statically-Typed, Purely Functional Programming...
John De Goes
 
NTU ML TENSORFLOW
NTU ML TENSORFLOWNTU ML TENSORFLOW
NTU ML TENSORFLOW
Mark Chang
 
Numpy - Array.pdf
Numpy - Array.pdfNumpy - Array.pdf
Numpy - Array.pdf
AnkitaArjunDevkate
 
Magic 8 ball putting it all together
Magic 8 ball  putting it all togetherMagic 8 ball  putting it all together
Magic 8 ball putting it all together
geekinlibrariansclothing
 
Presentation slides for my simulation course at Dauphine
Presentation slides for my simulation course at DauphinePresentation slides for my simulation course at Dauphine
Presentation slides for my simulation course at Dauphine
Christian Robert
 
Secure and privacy-preserving data transmission and processing using homomorp...
Secure and privacy-preserving data transmission and processing using homomorp...Secure and privacy-preserving data transmission and processing using homomorp...
Secure and privacy-preserving data transmission and processing using homomorp...
DefCamp
 
Python Tutorial
Python TutorialPython Tutorial
Python Tutorial
Eueung Mulyana
 
Week3
Week3Week3
Week3
준성 조
 
コード
コードコード
コード
technologicarts
 
Memorization of Various Calculator shortcuts
Memorization of Various Calculator shortcutsMemorization of Various Calculator shortcuts
Memorization of Various Calculator shortcuts
PrincessNorberte
 
ch05-program-logic-indefinite-loops.ppt
ch05-program-logic-indefinite-loops.pptch05-program-logic-indefinite-loops.ppt
ch05-program-logic-indefinite-loops.ppt
Mahyuddin8
 
Data monsters probablistic data structures
Data monsters probablistic data structuresData monsters probablistic data structures
Data monsters probablistic data structures
GreenM
 
Τα Πολύ Βασικά για την Python
Τα Πολύ Βασικά για την PythonΤα Πολύ Βασικά για την Python
Τα Πολύ Βασικά για την Python
Moses Boudourides
 
C Language Lecture 18
C Language Lecture 18C Language Lecture 18
C Language Lecture 18
Shahzaib Ajmal
 

Similar to chapter5.pdf (18)

error 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docx
error 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docxerror 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docx
error 2.pdf101316, 6(46 PM01_errorPage 1 of 5http.docx
 
Python
PythonPython
Python
 
PyCon2009_AI_Alt
PyCon2009_AI_AltPyCon2009_AI_Alt
PyCon2009_AI_Alt
 
Kotlin for Android Developers - 2
Kotlin for Android Developers - 2Kotlin for Android Developers - 2
Kotlin for Android Developers - 2
 
The Easy-Peasy-Lemon-Squeezy, Statically-Typed, Purely Functional Programming...
The Easy-Peasy-Lemon-Squeezy, Statically-Typed, Purely Functional Programming...The Easy-Peasy-Lemon-Squeezy, Statically-Typed, Purely Functional Programming...
The Easy-Peasy-Lemon-Squeezy, Statically-Typed, Purely Functional Programming...
 
NTU ML TENSORFLOW
NTU ML TENSORFLOWNTU ML TENSORFLOW
NTU ML TENSORFLOW
 
Numpy - Array.pdf
Numpy - Array.pdfNumpy - Array.pdf
Numpy - Array.pdf
 
Magic 8 ball putting it all together
Magic 8 ball  putting it all togetherMagic 8 ball  putting it all together
Magic 8 ball putting it all together
 
Presentation slides for my simulation course at Dauphine
Presentation slides for my simulation course at DauphinePresentation slides for my simulation course at Dauphine
Presentation slides for my simulation course at Dauphine
 
Secure and privacy-preserving data transmission and processing using homomorp...
Secure and privacy-preserving data transmission and processing using homomorp...Secure and privacy-preserving data transmission and processing using homomorp...
Secure and privacy-preserving data transmission and processing using homomorp...
 
Python Tutorial
Python TutorialPython Tutorial
Python Tutorial
 
Week3
Week3Week3
Week3
 
コード
コードコード
コード
 
Memorization of Various Calculator shortcuts
Memorization of Various Calculator shortcutsMemorization of Various Calculator shortcuts
Memorization of Various Calculator shortcuts
 
ch05-program-logic-indefinite-loops.ppt
ch05-program-logic-indefinite-loops.pptch05-program-logic-indefinite-loops.ppt
ch05-program-logic-indefinite-loops.ppt
 
Data monsters probablistic data structures
Data monsters probablistic data structuresData monsters probablistic data structures
Data monsters probablistic data structures
 
Τα Πολύ Βασικά για την Python
Τα Πολύ Βασικά για την PythonΤα Πολύ Βασικά για την Python
Τα Πολύ Βασικά για την Python
 
C Language Lecture 18
C Language Lecture 18C Language Lecture 18
C Language Lecture 18
 

Recently uploaded

storyboard: Victor and Verlin discussing about top hat
storyboard: Victor and Verlin discussing about top hatstoryboard: Victor and Verlin discussing about top hat
storyboard: Victor and Verlin discussing about top hat
LyneSun
 
一比一原版美国亚利桑那大学毕业证(ua毕业证书)如何办理
一比一原版美国亚利桑那大学毕业证(ua毕业证书)如何办理一比一原版美国亚利桑那大学毕业证(ua毕业证书)如何办理
一比一原版美国亚利桑那大学毕业证(ua毕业证书)如何办理
homgo
 
一比一原版(QUT毕业证)昆士兰科技大学毕业证成绩单如何办理
一比一原版(QUT毕业证)昆士兰科技大学毕业证成绩单如何办理一比一原版(QUT毕业证)昆士兰科技大学毕业证成绩单如何办理
一比一原版(QUT毕业证)昆士兰科技大学毕业证成绩单如何办理
zeyhe
 
Complete Lab 123456789123456789123456789
Complete Lab 123456789123456789123456789Complete Lab 123456789123456789123456789
Complete Lab 123456789123456789123456789
vickyvikas51556
 
This is a test powerpoint!!!!!!!!!!!!!!!
This is a test powerpoint!!!!!!!!!!!!!!!This is a test powerpoint!!!!!!!!!!!!!!!
This is a test powerpoint!!!!!!!!!!!!!!!
briannedpegg
 
Portfolio of my work as my passion and skills
Portfolio of my work as my passion and skillsPortfolio of my work as my passion and skills
Portfolio of my work as my passion and skills
waljorylypil626
 
In Focus_ The Evolution of Boudoir Photography in NYC.pdf
In Focus_ The Evolution of Boudoir Photography in NYC.pdfIn Focus_ The Evolution of Boudoir Photography in NYC.pdf
In Focus_ The Evolution of Boudoir Photography in NYC.pdf
Boudoir Photography by Your Hollywood Portrait
 
All the images mentioned in 'See What You're Missing'
All the images mentioned in 'See What You're Missing'All the images mentioned in 'See What You're Missing'
All the images mentioned in 'See What You're Missing'
Dave Boyle
 
哪里购买美国乔治城大学毕业证硕士学位证书原版一模一样
哪里购买美国乔治城大学毕业证硕士学位证书原版一模一样哪里购买美国乔治城大学毕业证硕士学位证书原版一模一样
哪里购买美国乔治城大学毕业证硕士学位证书原版一模一样
tc73868
 
2024 MATFORCE Youth Poster Contest Winners
2024 MATFORCE Youth Poster Contest Winners2024 MATFORCE Youth Poster Contest Winners
2024 MATFORCE Youth Poster Contest Winners
matforce
 
FinalA1LessonPlanMaking.docxdvdnlskdnvsldkvnsdkvn
FinalA1LessonPlanMaking.docxdvdnlskdnvsldkvnsdkvnFinalA1LessonPlanMaking.docxdvdnlskdnvsldkvnsdkvn
FinalA1LessonPlanMaking.docxdvdnlskdnvsldkvnsdkvn
abbieharman
 
Heart Touching Romantic Love Shayari In English with Images
Heart Touching Romantic Love Shayari In English with ImagesHeart Touching Romantic Love Shayari In English with Images
Heart Touching Romantic Love Shayari In English with Images
Short Good Quotes
 
❼❷⓿❺❻❷❽❷❼❽ Dpboss Matka ! Fix Satta Matka ! Matka Result ! Matka Guessing ! ...
❼❷⓿❺❻❷❽❷❼❽  Dpboss Matka ! Fix Satta Matka ! Matka Result ! Matka Guessing ! ...❼❷⓿❺❻❷❽❷❼❽  Dpboss Matka ! Fix Satta Matka ! Matka Result ! Matka Guessing ! ...
❼❷⓿❺❻❷❽❷❼❽ Dpboss Matka ! Fix Satta Matka ! Matka Result ! Matka Guessing ! ...
❼❷⓿❺❻❷❽❷❼❽ Dpboss Kalyan Satta Matka Guessing Matka Result Main Bazar chart
 
Barbie Made To Move Skin Tones Matches.pptx
Barbie Made To Move Skin Tones Matches.pptxBarbie Made To Move Skin Tones Matches.pptx
Barbie Made To Move Skin Tones Matches.pptx
LinaCosta15
 
My storyboard for the short film "Maatla".
My storyboard for the short film "Maatla".My storyboard for the short film "Maatla".
My storyboard for the short film "Maatla".
AlejandroGuarnGutirr
 
Cherries 32 collection of colorful paintings
Cherries 32 collection of colorful paintingsCherries 32 collection of colorful paintings
Cherries 32 collection of colorful paintings
sandamichaela *
 
Fashionista Chic Couture Mazes and Coloring AdventureA
Fashionista Chic Couture Mazes and Coloring AdventureAFashionista Chic Couture Mazes and Coloring AdventureA
Fashionista Chic Couture Mazes and Coloring AdventureA
julierjefferies8888
 
➒➌➎➏➑➐➋➑➐➐ Dpboss Satta Matka Matka Guessing Kalyan Chart Indian Matka Satta ...
➒➌➎➏➑➐➋➑➐➐ Dpboss Satta Matka Matka Guessing Kalyan Chart Indian Matka Satta ...➒➌➎➏➑➐➋➑➐➐ Dpboss Satta Matka Matka Guessing Kalyan Chart Indian Matka Satta ...
➒➌➎➏➑➐➋➑➐➐ Dpboss Satta Matka Matka Guessing Kalyan Chart Indian Matka Satta ...
➒➌➎➏➑➐➋➑➐➐Dpboss Matka Guessing Satta Matka Kalyan Chart Indian Matka
 
一比一原版美国加州大学圣地亚哥分校毕业证(ucsd毕业证书)如何办理
一比一原版美国加州大学圣地亚哥分校毕业证(ucsd毕业证书)如何办理一比一原版美国加州大学圣地亚哥分校毕业证(ucsd毕业证书)如何办理
一比一原版美国加州大学圣地亚哥分校毕业证(ucsd毕业证书)如何办理
taqyea
 
Ealing London Independent Photography meeting - June 2024
Ealing London Independent Photography meeting - June 2024Ealing London Independent Photography meeting - June 2024
Ealing London Independent Photography meeting - June 2024
Sean McDonnell
 

Recently uploaded (20)

storyboard: Victor and Verlin discussing about top hat
storyboard: Victor and Verlin discussing about top hatstoryboard: Victor and Verlin discussing about top hat
storyboard: Victor and Verlin discussing about top hat
 
一比一原版美国亚利桑那大学毕业证(ua毕业证书)如何办理
一比一原版美国亚利桑那大学毕业证(ua毕业证书)如何办理一比一原版美国亚利桑那大学毕业证(ua毕业证书)如何办理
一比一原版美国亚利桑那大学毕业证(ua毕业证书)如何办理
 
一比一原版(QUT毕业证)昆士兰科技大学毕业证成绩单如何办理
一比一原版(QUT毕业证)昆士兰科技大学毕业证成绩单如何办理一比一原版(QUT毕业证)昆士兰科技大学毕业证成绩单如何办理
一比一原版(QUT毕业证)昆士兰科技大学毕业证成绩单如何办理
 
Complete Lab 123456789123456789123456789
Complete Lab 123456789123456789123456789Complete Lab 123456789123456789123456789
Complete Lab 123456789123456789123456789
 
This is a test powerpoint!!!!!!!!!!!!!!!
This is a test powerpoint!!!!!!!!!!!!!!!This is a test powerpoint!!!!!!!!!!!!!!!
This is a test powerpoint!!!!!!!!!!!!!!!
 
Portfolio of my work as my passion and skills
Portfolio of my work as my passion and skillsPortfolio of my work as my passion and skills
Portfolio of my work as my passion and skills
 
In Focus_ The Evolution of Boudoir Photography in NYC.pdf
In Focus_ The Evolution of Boudoir Photography in NYC.pdfIn Focus_ The Evolution of Boudoir Photography in NYC.pdf
In Focus_ The Evolution of Boudoir Photography in NYC.pdf
 
All the images mentioned in 'See What You're Missing'
All the images mentioned in 'See What You're Missing'All the images mentioned in 'See What You're Missing'
All the images mentioned in 'See What You're Missing'
 
哪里购买美国乔治城大学毕业证硕士学位证书原版一模一样
哪里购买美国乔治城大学毕业证硕士学位证书原版一模一样哪里购买美国乔治城大学毕业证硕士学位证书原版一模一样
哪里购买美国乔治城大学毕业证硕士学位证书原版一模一样
 
2024 MATFORCE Youth Poster Contest Winners
2024 MATFORCE Youth Poster Contest Winners2024 MATFORCE Youth Poster Contest Winners
2024 MATFORCE Youth Poster Contest Winners
 
FinalA1LessonPlanMaking.docxdvdnlskdnvsldkvnsdkvn
FinalA1LessonPlanMaking.docxdvdnlskdnvsldkvnsdkvnFinalA1LessonPlanMaking.docxdvdnlskdnvsldkvnsdkvn
FinalA1LessonPlanMaking.docxdvdnlskdnvsldkvnsdkvn
 
Heart Touching Romantic Love Shayari In English with Images
Heart Touching Romantic Love Shayari In English with ImagesHeart Touching Romantic Love Shayari In English with Images
Heart Touching Romantic Love Shayari In English with Images
 
❼❷⓿❺❻❷❽❷❼❽ Dpboss Matka ! Fix Satta Matka ! Matka Result ! Matka Guessing ! ...
❼❷⓿❺❻❷❽❷❼❽  Dpboss Matka ! Fix Satta Matka ! Matka Result ! Matka Guessing ! ...❼❷⓿❺❻❷❽❷❼❽  Dpboss Matka ! Fix Satta Matka ! Matka Result ! Matka Guessing ! ...
❼❷⓿❺❻❷❽❷❼❽ Dpboss Matka ! Fix Satta Matka ! Matka Result ! Matka Guessing ! ...
 
Barbie Made To Move Skin Tones Matches.pptx
Barbie Made To Move Skin Tones Matches.pptxBarbie Made To Move Skin Tones Matches.pptx
Barbie Made To Move Skin Tones Matches.pptx
 
My storyboard for the short film "Maatla".
My storyboard for the short film "Maatla".My storyboard for the short film "Maatla".
My storyboard for the short film "Maatla".
 
Cherries 32 collection of colorful paintings
Cherries 32 collection of colorful paintingsCherries 32 collection of colorful paintings
Cherries 32 collection of colorful paintings
 
Fashionista Chic Couture Mazes and Coloring AdventureA
Fashionista Chic Couture Mazes and Coloring AdventureAFashionista Chic Couture Mazes and Coloring AdventureA
Fashionista Chic Couture Mazes and Coloring AdventureA
 
➒➌➎➏➑➐➋➑➐➐ Dpboss Satta Matka Matka Guessing Kalyan Chart Indian Matka Satta ...
➒➌➎➏➑➐➋➑➐➐ Dpboss Satta Matka Matka Guessing Kalyan Chart Indian Matka Satta ...➒➌➎➏➑➐➋➑➐➐ Dpboss Satta Matka Matka Guessing Kalyan Chart Indian Matka Satta ...
➒➌➎➏➑➐➋➑➐➐ Dpboss Satta Matka Matka Guessing Kalyan Chart Indian Matka Satta ...
 
一比一原版美国加州大学圣地亚哥分校毕业证(ucsd毕业证书)如何办理
一比一原版美国加州大学圣地亚哥分校毕业证(ucsd毕业证书)如何办理一比一原版美国加州大学圣地亚哥分校毕业证(ucsd毕业证书)如何办理
一比一原版美国加州大学圣地亚哥分校毕业证(ucsd毕业证书)如何办理
 
Ealing London Independent Photography meeting - June 2024
Ealing London Independent Photography meeting - June 2024Ealing London Independent Photography meeting - June 2024
Ealing London Independent Photography meeting - June 2024
 

chapter5.pdf