Random numbers in Python

1 / 12

# Random numbers in Python - PowerPoint PPT Presentation

Random numbers in Python. Nobody knows what’s next. Deterministic machines. That means that computers do the same predictable thing every time you give them the same instructions we WANT this – we don't want random happenings - most of the time when would you NOT want this? games

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'Random numbers in Python' - jenette-mclaughlin

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### Random numbers in Python

Nobody knows what’s next...

Deterministic machines
• That means that computers do the same predictable thing every time you give them the same instructions
• we WANT this – we don't want random happenings - most of the time
• when would you NOT want this?
• games
• simulations of reality – traffic flows, nuclear explosions, star formation
• Cryptography!
What is "random"?
• A “real” random number is generated by a real world event like an atom decaying or not in a certain time
• Hard to do that in a computer
• Best we can do with a deterministic machine is make “pseudorandom” numbers
• They are “good enough”
“good enough”?
• A “good” random number is one that is distributed evenly in its range
• If you were rolling a die, you want numbers from 1 to 6 to be equally likely to show up
• This is over the “long run”, not each individual trial
Lots of research
• Lots has been done on random numbers
• Trying to get faster algorithms
• With larger cycles – all algorithms will eventually start repeating but the best ones not before a few million numbers at least
• Very heavy statistics and mathematics in the latest algorithms
A simple one – “mid square”
• Square it
• Take the “middle” of it – trim off some digits at front and end
• That’s the random number
• Repeat the process by feeding the number just generated back in as the starting number next time
An example
• 12345 squared = 152399025
• chop it off and get 23990
• 23990 squared = 575520100
• chop it off and get 55201
• 55201 squared = 3047150401
• chop it off and get 47150
• And so on
Properties of an RNG
• Give the algorithm a DIFFERENT seed to start with and what comes out?
• Give the algorithm the SAME seed to start with and what comes out?
Syntax
• from random import *
• Use seed(x) to set the initial seed (x is usually an integer)
• randrange(stop) gets random integer from 0 to stop-1
• randrange(start,stop) to get random integer number from start to stop-1, inclusive
Syntax
• Use randrange(start, stop, step) to get random numbers from start to stop-1, with a step size
• Use random() to get a floating point number in the range [0.0, 1.0) (meaning that it may generate 0, it will not generate 1.0) (useful for probabilities, 0 means “will not happen”, 1 means “certain to happen”)
• seed(23) will always give you the same sequence of random numbers – good when testing!
• Asking the user for a number and then using it as the seed - works but is a bit aggravating to the user
• If you don’t set seed at all, will use system time – different for every run
Examples
• choose a random word from a list

words = [“cat”, “hat”, “fox”, “house”, ”red”]

print(words[randrange(len(words))])

• roll some dice

for i in range(10):

print (randrange(1,7), randrange(1,7))