Random numbers in C++ Nobody knows what’s next....

Slides:



Advertisements
Similar presentations
Savitch N/A Randomness In Programming Simulations
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.
 Monday, 10/28/02, Slide #1 CS106 Introduction to CS1 Monday, 10/28/02  QUESTIONS on HW 03??  Today: Generating random numbers  Reading & exercises:
Random Number Generators. Why do we need random variables? random components in simulation → need for a method which generates numbers that are random.
Understanding Randomness
Pseudorandom Number Generators
Statistics.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 12 – Craps Game Application: Introducing Random.
1 Lab Session-9 CSIT-121 Fall 2003 w Random Number Generation w Designing a Game.
1 Random numbers Random  completely unpredictable. No way to determine in advance what value will be chosen from a set of equally probable elements. Impossible.
CSCE Monte Carlo Methods When you can’t do the math, simulate the process with random numbers Numerical integration to get areas/volumes Particle.
Random numbers in Python Nobody knows what’s next...
1 Building Java Programs Chapter 5 Lecture 5-2: Random Numbers reading: 5.1, 5.6.
Craps!. Example: A Game of Chance Craps simulator Rules – Roll two dice 7 or 11 on first throw, player wins 2, 3, or 12 on first throw, player loses 4,
Section 5.1 What is Probability? 5.1 / 1. Probability Probability is a numerical measurement of likelihood of an event. The probability of any event is.
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.
Simulation Examples ~ By Hand ~ Using Excel
Random Numbers CSE 331 Section 2 James Daly. Randomness Most algorithms we’ve talked about have been deterministic The same inputs always give the same.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 7: One More Loop Problem, Generating “random” values, Midterm Review.
CS433 Modeling and Simulation Lecture 15 Random Number Generator Dr. Anis Koubâa 24 May 2009 Al-Imam Mohammad Ibn Saud Islamic University College Computer.
1-1 Copyright © 2015, 2010, 2007 Pearson Education, Inc. Chapter 10, Slide 1 Chapter 10 Understanding Randomness.
Understanding Randomness Chapter 11. Why Be Random? What is it about chance outcomes being random that makes random selection seem fair? Two things: –
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11 Understanding Randomness.
Random Number Generators 1. Random number generation is a method of producing a sequence of numbers that lack any discernible pattern. Random Number Generators.
Iterative Constructs Review l What are named constants? Why are they needed? l What is a block? What is special about declaring a variable inside a block?
Monte Carlo Methods.
Understanding Randomness
Today’s Lecture Predefined Functions. Introduction to Functions  Reuse Issue  Building Blocks of Programs  Two types of functions  Predefined  Programmer.
Copyright © 2010, 2007, 2004 Pearson Education, Inc. Chapter 11 Understanding Randomness.
 2008 Pearson Education, Inc. All rights reserved Case Study: Random Number Generation C++ Standard Library function rand – Introduces the element.
C++ Programming Lecture 10 Functions – Part II
1 Building Java Programs Chapter 5 Lecture 5-2: Random Numbers reading: 5.1, 5.6.
Loops Wrap Up 10/21/13. Topics *Sentinel Loops *Nested Loops *Random Numbers.
Slide Understanding Randomness.  What is it about chance outcomes being random that makes random selection seem fair? Two things:  Nobody can.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions (Header files and Library Functions) Outline.
CS221 Random Numbers. Random numbers are often very important in programming Suppose you are writing a program to play the game of roulette The numbers.
Simulating Experiments on the TI Section Starter Use the random integer generator in your calculator to choose an SRS of 5 students from.
Understanding Randomness.  Many phenomena in the world are random: ◦ Nobody can guess the outcome before it happens. ◦ When we want things to be fair,
CHAPTER 5 Simulation Modeling. Introduction In many situations a modeler is unable to construct an analytic (symbolic) model adequately explaining the.
1D Arrays and Random Numbers Artem A. Lenskiy, PhD May 26, 2014.
CSci 162 Lecture 7 Martin van Bommel. Random Numbers Until now, all programs have behaved deterministically - completely predictable and repeatable based.
Invasion Percolation: Randomness Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Chapter 10 Understanding Randomness. Why Be Random? What is it about chance outcomes being random that makes random selection seem fair? Two things: –
UNIT 11 Random Numbers.
ONE DIMENSIONAL ARRAYS AND RANDOM NUMBERS. Introduction In addition to arrays and structures, C supports creation and manipulation of the following data.
1 Chapter 11 Understanding Randomness. 2 Why Be Random? What is it about chance outcomes being random that makes random selection seem fair? Two things:
1 Generating Random Numbers Textbook ch.6, pg
MR. CRONE Generating Random Numbers. Random Numbers Many programs require the computer to generate random numbers Random numbers are used in many applications.
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.
Switch Case, Enums, and Random Numbers CS 242 Tarik Booker California State University, Los Angeles.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
1 Building Java Programs Chapter 5 Lecture 11: Random Numbers reading: 5.1, 5.6.
Introduction to programming in java Lecture 16 Random.
Monte Carlo Methods Some example applications in C++
Why they aren't really random
Python Random numbers Peter Wad Sackett.
Random Numbers Until now, all programs have behaved deterministically - completely predictable and repeatable based on input values Some applications.
Number guessing game Pick a random number between 1 and 10
Iterative Constructs Review
CSC113: Computer Programming (Theory = 03, Lab = 01)
CS1010 Programming Methodology
Random numbers Taken from notes by Dr. Neil Moore
Random Number Generation
Pseudo-random numbers
Iterative Constructs Review
Python Random numbers Peter Wad Sackett.
CS150 Introduction to Computer Science 1
Presentation transcript:

Random numbers in C++ 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” 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

An example squared = chop it off and get squared = chop it off and get squared = chop it off and get 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 Include the cstdlib library to get the functions Use srand(seed); to set the initial seed (seed must be an integer) Use rand() to get the next random number  returns an integer between 0 and RAND_MAX Call srand ONCE per program, call rand many times

Seeds to start with srand(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 Using the time function is most flexible (see example on web page)

rand() and RAND_MAX rand() returns a integer between 0 and RAND_MAX (a constant defined in cstdlib) What if you need an integer number between 1 and 6? rand() % What if you need a number between 0 and 1 (i.e, a fraction) ? rand() / RAND_MAX (but watch your TYPE here!)