CSci 162 Lecture 7 Martin van Bommel. Random Numbers Until now, all programs have behaved deterministically - completely predictable and repeatable based.

Slides:



Advertisements
Similar presentations
1 Chapter Eight Designing Libraries. 2 Programming Complexity As long as we continue to solve problems of ever-increasing sophistication, the process.
Advertisements

Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 6.
Chapter 13 – Boot Strap Method. Boot Strapping It is a computer simulation to generate random numbers from a sample. In Excel, it can simulate 5000 different.
Random variables 1. Note  there is no chapter in the textbook that corresponds to this topic 2.
Clear your desk for your quiz. Unit 2 Day 8 Expected Value Average expectation per game if the game is played many times Can be used to evaluate and.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 A Simulation Problem Length of room Width of room Start.
Exercise 4 1. Write a program that simulates coin tossing. For each toss of the coin the program should print Heads or Tails. Let the program toss the.
Probability Predictions Ch. 1, Act. 5. Probability The study of random events. Random events are things that happen without predictability – e.g. the.
Take out a coin! You win 4 dollars for heads, and lose 2 dollars for tails.
Copyright © Cengage Learning. All rights reserved. 8.6 Probability.
A Java API Package java.security  The Java Security Package contains classes and interfaces that are required by many Java programs.  This package is.
 Monday, 10/28/02, Slide #1 CS106 Introduction to CS1 Monday, 10/28/02  QUESTIONS on HW 03??  Today: Generating random numbers  Reading & exercises:
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 12 – Craps Game Application: Introducing Random.
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.
Random numbers in Python Nobody knows what’s next...
Section The Idea of Probability Statistics.
Chapter 16: Random Variables
Binomial & Geometric Random Variables §6-3. Goals: Binomial settings and binomial random variables Binomial probabilities Mean and standard deviation.
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.
Statistics Frequency and Distribution. We interrupt this lecture for the following… Significant digits You should not report numbers with more significant.
© 2008 McGraw-Hill Higher Education The Statistical Imagination Chapter 6. Probability Theory and the Normal Probability Distribution.
CSCI 125 & 161 Lecture 13 Martin van Bommel. Floating Point Data Floating point numbers are not exact Value 0.1 in binary is very close to 1/10, but not.
Approximate computations Jordi Cortadella Department of Computer Science.
Understanding Randomness Chapter 11. Why Be Random? What is it about chance outcomes being random that makes random selection seem fair? Two things: –
Binomial Experiment A binomial experiment (also known as a Bernoulli trial) is a statistical experiment that has the following properties:
1.3 Simulations and Experimental Probability (Textbook Section 4.1)
Chapter 16: Random Variables
Today’s Lecture Predefined Functions. Introduction to Functions  Reuse Issue  Building Blocks of Programs  Two types of functions  Predefined  Programmer.
 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
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.
Introduction to Programming Lecture 11. ARRAYS They are special kind of data type They are special kind of data type They are like data structures in.
Introduction to the Essentials of Excel COMP 066.
Computer Simulation. The Essence of Computer Simulation A stochastic system is a system that evolves over time according to one or more probability distributions.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
Random numbers in C++ Nobody knows what’s next....
Bernoulli Trials, Geometric and Binomial Probability models.
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
Chapter 10 Understanding Randomness. Why Be Random? What is it about chance outcomes being random that makes random selection seem fair? Two things: –
Chapter 6—Objects and Classes The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Objects and Classes C H A P T E R 6 To beautify.
CSC317 1 Randomized algorithms Hiring problem We always want the best hire for a job! Using employment agency to send one candidate at a time Each day,
CSE202: Lecture 13The Ohio State University1 Function Scope.
Section The Idea of Probability AP Statistics
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.
Module 9.4 Random Numbers from Various Distributions -MC requires the use of unbiased random numbers.
Switch Case, Enums, and Random Numbers CS 242 Tarik Booker California State University, Los Angeles.
1 Building Java Programs Chapter 5 Lecture 11: Random Numbers reading: 5.1, 5.6.
Introduction to programming in java Lecture 16 Random.
Adding Probabilities 12-5
Monte Carlo Methods Some example applications in C++
Negative Binomial Experiment
Probability 100% 50% 0% ½ Will happen Won’t happen
Probability Predictions Ch. 1, Act. 5.
Why they aren't really random
© 2016 Pearson Education, Ltd. All rights reserved.
Random Numbers Until now, all programs have behaved deterministically - completely predictable and repeatable based on input values Some applications.
CMPT 201 Functions.
Conditional Probability
Expected Value.
The Random Class and its Methods
PROBABILITY The probability of an event is a value that describes the chance or likelihood that the event will happen or that the event will end with.
Chapter 17 Thinking about Chance.
Introduction to Programming
Probability Trees By Anthony Stones.
Expected Value.
Random Variable Random Variable – Numerical Result Determined by the Outcome of a Probability Experiment. Ex1: Roll a Die X = # of Spots X | 1.
A First Book of ANSI C Fourth Edition
Section 6.1 The Idea of Probability
Expected Value.
Presentation transcript:

CSci 162 Lecture 7 Martin van Bommel

Random Numbers Until now, all programs have behaved deterministically - completely predictable and repeatable based on input values Some applications (e.g. games) require unpredictable behavior - nondeterministic One method for nondeterminism die toss where one of several outcomes possible

Random Numbers Number is random if there is no way to determine in advance what value it will have among a set of equally probable possibilites E.g. on a standard unloaded die –random number between 1 and 6 equally possible Cannot generate number randomly on computer –uses deterministic behavior

Pseudo-random Numbers Use a deterministic procedure to generate “random” numbers Numbers must appear random –behave like random numbers statistically –be sufficiently difficult to predict in advance Random numbers generated by an algorithmic process by a computer are called pseudo-random numbers

Pseudo-random number in C++ includes function rand and constant RAND_MAX prototype int rand(); generates pseudo-random number between 0 and RAND_MAX, inclusive Value of RAND_MAX depends on system Ours uses but your program should not rely on a given value but on the symbolic constant

Using “rand” to flip a coin Want to use the result of rand that is a number between 0 and RAND_MAX to generate Heads or Tails Use if (rand() <= RAND_MAX / 2) cout << ”Heads”; else cout << ”Tails”;

Wrong Method to flip a coin Why not use: if (rand() % 2 == 0) cout << ”Heads”; etc. No guarantee of distribution of even and odd numbers, only even distribution along the range 0 to RAND_MAX

Using “rand” to Roll a Die int RollDie(void) { if (rand() < RAND_MAX / 6) return 1; if (rand() < RAND_MAX*2/6) return 2; if (rand() < RAND_MAX*3/6) return 3; if (rand() < RAND_MAX*4/6) return 4; if (rand() < RAND_MAX*5/6) return 5; return 6; } Unfortunately uses new value of “rand” in each “if”, thus losing original.

Using “rand” to Roll a Die (2) int RollDie() { int r = rand(); if (r < RAND_MAX / 6) return 1; if (r < RAND_MAX*2/6) return 2; if (r < RAND_MAX*3/6) return 3; if (r < RAND_MAX*4/6) return 4; if (r < RAND_MAX*5/6) return 5; return 6; } Unfortunately RAND_MAX*2/6 gives strange value due to integer arithmetic Better to use RAND_MAX/6.0*2.0

Generalize the Problem Want a function to choose random number between two values, inclusive int RandomInteger(int low, int high); Four-step process 1. Normalize rand to number d where 0 <= d < 1 2. Scale d by multiplying by size of desired range 3. Truncate number back to an integer 4. Translate integer so range begins at lower bound

RandomInteger int RandomInteger(int low, int high) { int k; double d; d = rand() / ((double) RAND_MAX + 1.0); k = (int) (d * (high - low + 1)); return (low + k); }