Random numbers Taken from notes by Dr. Neil Moore

Slides:



Advertisements
Similar presentations
ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
Advertisements

Random Number Generation Graham Netherton Logan Stelly.
Random variables 1. Note  there is no chapter in the textbook that corresponds to this topic 2.
Python (yay!) November 16, Unit 7. Recap We can store values in variables using an assignment statement >>>x = We can get input from the user using.
Pseudorandom Number Generators
Chapter 9 Simulation and top-down design.  Simply put, simulation means using computing processes to simulate real-world situations to obtain information.
Random numbers in Python Nobody knows what’s next...
Chapter 4: Looping CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
Fundamentals of Python: From First Programs Through Data Structures
Random Number Generators CISC/QCSE 810. What is random? Flip 10 coins: how many do you expect will be heads? Measure 100 people: how are their heights.
Fundamentals of Python: First Programs
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Chapter 11 – Understanding Randomness 1. What is a random event? Nobody can guess the outcome before it happens. Let’s try an experiment. On the next page.
Understanding Randomness Chapter 11. Why Be Random? What is it about chance outcomes being random that makes random selection seem fair? Two things: –
Pseudo-random generators Random Number Generating There are three types of generators table look-up generators hardware generators algorithmic (software)
Monte Carlo Methods.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Loops Wrap Up 10/21/13. Topics *Sentinel Loops *Nested Loops *Random Numbers.
Random numbers in C++ Nobody knows what’s next....
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
Chapter 10 Understanding Randomness. Why Be Random? What is it about chance outcomes being random that makes random selection seem fair? Two things: –
CS 115 Lecture 9 Augmented assignment; Boolean logic; random numbers Taken from notes by Dr. Neil Moore.
Statistics 11 Understanding Randomness. Example If you had a coin from someone, that they said ended up heads more often than tails, how would you test.
CS 115 Lecture 5 Math library; building a project Taken from notes by Dr. Neil Moore.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
List Algorithms Taken from notes by Dr. Neil Moore & Dr. Debby Keen
CS 115 Lecture Augmented assignment; Boolean logic; random numbers
Topics Introduction to Functions Defining and Calling a Void Function
CSc 110, Spring 2017 Lecture 12: Random Numbers
Recap: If, elif, else If <True condition>:
Repetition Structures
Why they aren't really random
Formatting Output.
Python Random numbers Peter Wad Sackett.
CS 115 Lecture 8 Structured Programming; for loops
CSc 110, Autumn 2016 Lecture 13: Random Numbers
Understanding Randomness
Topics Introduction to Repetition Structures
A cryptographically secure pseudorandom number generator for Julia
Structured Programming; for loops Taken from notes by Dr. Neil Moore
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Cryptography Lecture 4.
Understanding Randomness
Understanding Randomness
Learning to Program in Python
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
List Algorithms Taken from notes by Dr. Neil Moore
The Math class The java.lang.Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square.
Random numbers What does it mean for a number to be random?
Random vs pseudo random
CS 475/575 Slide set 4 M. Overstreet Old Dominion University
Random Numbers In today’s lesson we will look at:
Repetition Structures
Coding Concepts (Sub- Programs)
Loops CIS 40 – Introduction to Programming in Python
Coding Concepts (Basics)
CS 115 Lecture 7 Augmented assignment; Boolean logic; random numbers
Cryptography and Network Security Chapter 7
Python Random numbers Peter Wad Sackett.
Writing Functions( ) (Part 4)
Last Class We Covered What makes “good code” good Top down design
Python programming exercise
Random number generators
Counting Discrete Mathematics.
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Recursion Taken from notes by Dr. Neil Moore
For loops Taken from notes by Dr. Neil Moore
Functions Taken from notes by Dr. Neil Moore & Dr. Debby Keen
COMPUTING.
Presentation transcript:

Random numbers Taken from notes by Dr. Neil Moore CS 115 Lecture Random numbers Taken from notes by Dr. Neil Moore

Random numbers We’ve seen some modules or libraries in Python: math graphics A library is a collection of pre-written code indented to be re-used. Python comes with a couple hundred modules And there are thousands more third-party modules Let’s look at another built-in module: random

Randomness The random module provides functions for generating random numbers Computers are deterministic: The same instructions given the same data (input) yields the same results every time Usually this is what we want! When would we want a program to do different things every time it’s run? Games Simulations of the real world: traffic, weather, galaxies colliding, … Cryptography For these kinds of problems we want random numbers But how can we get real randomness in a deterministic machine? There are ways (hooking up a radiation source and look for decays, etc. … ) but it’s not needed most of the time Pseudorandom numbers are usually good enough for our purposes

Randomness What does “random” mean? An even distribution of results If we’re rolling a die, we expect 1 about 1/6th of the time And 2 about 1/6th of the time, 3 about 1/6th … Uniform distribution: each possibility is equally likely This does NOT mean exactly uniform results! If you roll a die six times, you will get some number twice What it means is that over a large number of tests, the distribution gets closer to 1/6th each An even distribution isn’t enough to be “random” What if the die always rolled 1,2,3,4,5,6, 1,2,3,4,5,6,… in that order? Random numbers should be unpredictable Specifically seeing several numbers in the series should not let us guess the next one

Pseudorandom numbers Pseudorandom numbers use a deterministic algorithm (a random number generator, RNG) to generate numbers that appear to be random: Approximately uniform Hard to predict (but theoretically not impossible) ALL RNGs will repeat eventually, a good one does not for a very long time A lot of research has gone (and goes) into RNGs Linear congruential, alternating shift generator, Mersenne twister, … The Art of Computer Programming spends half a book on RNGs. Why so much research? They are very important for security! Cryptograph uses random numbers for session keys (like automatically generated one-time passwords) If someone could predict the output of the RNG, they could predict the key and break in!

Randomness involves information Randomness involves information or the lack of it Imagine you are standing at the top of a 50-story building. If someone asked you to predict what the traffic at ground-level would be, “when will the next car come around the corner?” you would be in a good position to make a prediction because you can see the streets for a long way If you were standing at ground level next to the same building, you have much less information and you could not make a very good prediction about the traffic With more information, things are less random; with less information, things seem more random That’s why the RNG numbers are called pseudo. With enough information, i.e. the RNG algorithm used and the seed, you could calculate the numbers just like the computer does. The numbers ARE predictable in that sense. Since we don’t usually have that info (or want to do that), the numbers seem random to us!

A good RNG What makes a “good” random number generator? The same features we mentioned earlier – uniform distribution, being unpredictable It must be quick to calculate – a typical game would use millions of them Most of them use integer arithmetic because it’s faster than floating point It must have a long cycle before it repeats EVERY RNG will eventually repeat if run long enough, but if the cycle is a few million numbers, it will seem “unpredictable” for most humans Why will it repeat? Imagine you had a “perfect” RNG which would generate every possible number the computer could represent (in no predictable order). That’s a finite number of numbers. What if you asked for another random number after it had done that? It HAS to give you a number it has already given before! It has no others to give.

Using Python’s random number library Python’s random number generator is in the random library import random or from random import * There are several functions in the library. https://docs.python.org/3/library/random.html Note the big red warning! This RNG should not be used for critical applications like banking! The simplest function is random chance = random() Gives a random floating point number in the range [0.0, 1.0) Notation: including 0.0, not including 1.0 Useful for probabilities, 1 means “will happen”, 0 means “will not happen” if random() < 0.7: # 70% chance to be True What if we want a random float in another range? You could multiply and add score = 90.0 * random() + 10.0 the range of this variable is [10.0, 100.0)

Using random() We want the program to print out a greeting 30% of the time it should be “hi!” 20% of the time it should be “hello” And 50% of the time it should be “How are you?” rnd = random() # gives a float number between 0 and 1 if rnd <= 0.3: print(“hi!”) elif rnd <= 0.5: #between 0.3 and 0.5, a space of 0.2 print(“hello”) else: print(“How are you?”)

Random integers We could multiply, add and type-cast to get a random integer from the random function. But there are functions in the library to do it for us. The randrange function Takes one to three arguments and returns an integer: randrange(stop): between zero (inclusive) and stop (exclusive! Not including stop) randrange(start, stop): [start, stop) between start (inclusive) and stop (exclusive) randrange(start, stop, step): Likewise, but only gives start plus a multiple of step

Random integers “Give me a random multiple of 10 between 0 and 100 inclusive.” score = randrange(0, 101, 10) What if we had written 100 instead? 100 is not included in the possible results Also: randint (a, b): Inclusive on both ends! The same as randrange (a, b+1) Always has two arguments, no more, no less Returns numbers in a range starting at a, up to and including b

Random choice Python can also choose randomly from a list of alternatives: sacrifice = choice([“time”, “money”, “quality”]) Must give a list of choices, in square brackets. Don’t forget the brackets! choice (“time”, “money”, “quality”) TypeError: choice(2) takes 2 positional arguments but four were given Can give a string as an argument instead: answer=choice(“ABCD”) Returns a random letter from the string Could get the same result with answer = “ABCD”[randrange(4)]

Seeding the RNG Sometimes it’s useful to be able to repeat the program exactly, with the same sequence of random numbers. Why? Reproducible simulations Cryptography: client and server might need the same numbers Testing programs (and games) We can specify the seed for the RNG seed(42) do it ONCE at the beginning of the program Now the sequence of numbers will be the same each time the program runs seed(43) gives a completely different random number sequence Not necessarily larger numbers (size of seed does not correlate with size of numbers) What if you never set a seed? Python picks one for you, based on the system time On some OSes it can use OS randomness instead Only set the seed ONCE per program!