Presentation is loading. Please wait.

Presentation is loading. Please wait.

Random numbers in Python Nobody knows what’s next...

Similar presentations


Presentation on theme: "Random numbers in Python Nobody knows what’s next..."— Presentation transcript:

1 Random numbers in Python Nobody knows what’s next...

2 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!

3 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”

4 “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

5 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

6 A simple one – “mid square” Take a number to start with (the “seed”) 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

7 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

8 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?

9 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

10 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”)

11 Seeds to start with 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

12 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))


Download ppt "Random numbers in Python Nobody knows what’s next..."

Similar presentations


Ads by Google