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.

Slides:



Advertisements
Similar presentations
Random variables 1. Note  there is no chapter in the textbook that corresponds to this topic 2.
Advertisements

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.
Advanced LABVIEW EE 2303.
Overview Reference parameters Documenting functions A game of craps. Design, code, test and document.
A Java API Package java.security  The Java Security Package contains classes and interfaces that are required by many Java programs.  This package is.
The Princeton Egg The Global Consciousness Project (video)The Global Consciousness Project (video) Princeton Egg Website Our Egg: PrincetonEgg.cppPrincetonEgg.cpp.
 Monday, 10/28/02, Slide #1 CS106 Introduction to CS1 Monday, 10/28/02  QUESTIONS on HW 03??  Today: Generating random numbers  Reading & exercises:
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions.
Programming Assignment 8 CS113 Spring Programming Assignment 8 Implement the sorting routine of your choice. Compare average performance of your.
© 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.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
Functions. 3Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece more manageable than the original program.
Random Number Generator. Using Software We can generate random number by: 1- table 2- hardware 3-- software Function to generate RN by SW is: 1-rand 2-
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.
1 Lecture 3 Part 1 Functions with math and randomness.
Function. Introduction Library function New defined function Random number generator Scope Inline function Function overload Function Function.
1 TAC2000/ Protocol Engineering and Application Research Laboratory (PEARL) MATH Functions in C Language.
DiceRoller DiceRoller (object class) and DiceRollerViewer client class: Write a DiceRoller class (similar to Yahtzee) to: Allow the person to initially.
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?
Week 7 - Wednesday.  What did we talk about last time?  scanf()  Memory allocation  malloc()  free()
CMSC 1041 Functions II Functions that return a value.
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.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions.
C++ Programming Lecture 10 Functions – Part II
Loops Wrap Up 10/21/13. Topics *Sentinel Loops *Nested Loops *Random Numbers.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Functions.
© 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.
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.
1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions 3.4Functions 3.5Function Definitions 3.6Function.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
EPSII 59:006 Spring Call-by-value example #include void increment(int); //prototype for increment function int main(void) { int a=1; printf("Value.
 for loop  while loop  do-while loop for (begin point; end point ; incrementation ) { //statements to be repeated }
Random numbers in C++ Nobody knows what’s next....
+ Arrays & Random number generator. + Introduction In addition to arrays and structures, C supports creation and manipulation of the following data structures:
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.
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.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions.
Function Call Stack and Activation Frame Stack Just like a pile of dishes Support Two operations push() pop() LIFO (Last-In, First-Out) data structure.
CSE202: Lecture 13The Ohio State University1 Function Scope.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
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.
Revision. Generating a pseudo-random number Necessary libraries: and Seeding: srand(time(NULL))  We set the seed based on the current time  NB! Time-dependent.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Introduction Program Components in C++ Math Library Functions Functions.
Objective of the lesson Use Blockly to make a dice for Snakes and Ladders All of you will: – Make an image which displays when you press a button Most.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
Introduction to programming in java Lecture 16 Random.
Static Variables. Function Scope  “Normal” local variables go out of scope and are deallocated when a function terminates.
Monte Carlo Methods Some example applications in C++
Probability Imagine tossing two coins and observing whether 0, 1, or 2 heads are obtained. It would be natural to guess that each of these events occurs.
© 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.
Number guessing game Pick a random number between 1 and 10
Iterative Constructs Review
CMPT 201 Functions.
CSC113: Computer Programming (Theory = 03, Lab = 01)
CS1010 Programming Methodology
The Random Class and its Methods
توابع در C++ قسمت اول اصول كامپيوتر 1.
Random Number Generation
Introduction to Programming
CS150 Introduction to Computer Science 1
Presentation transcript:

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 with any program! However we can generate seemingly random numbers, called pseudorandom numbers. The function rand() returns a non-negative number between 0 and RAND_MAX It is defined in stdlib.h

2 Random numbers /* * This program prints a list of 10 random * integers between 0 and RAND_MAX */ #include int main () { for (int i=0; i<10; i++) std::cout << rand() << std::endl; return 0; }

3 Random numbers The very first call to rand() uses an initial value to generate a pseudorandom number. That value is called the seed. Each subsequent call to rand() uses the result of the previous call as the starting value. rand() s rand() rand()

4 Random numbers Starting always with the same seed will give the same sequence of numbers. In order to get a different sequence every time the program is run, we must make sure to set a different seed every time. The only thing that changes between executions is the time  use the current time as the seed.

5 Random numbers Set the seed for rand() with the function: To set the current time as the seed, use the time() function: Libraries needed: #include /* for rand(), srand() */ #include /* for time() */ void srand(int seed) void srand((int) time(NULL))

6 Random numbers Example: int main () { srand((int)time(NULL)); for (int i=0; i<10; i++) { cout << "You rolled a " << rand()%6+1 << endl; } return 0; } CAREFUL! Only use srand() once and never place it in a loop. srand() sets the seed to the current second so if it is called repeatedly within the same second (e.g. due to a loop), it will keep resetting the seed to the same value.

7 Random numbers When should you not set a variable seed? When you develop your project, you may want to be able to get the same sequence of numbers for debugging purposes.

8 Random numbers What if we want a specific range? Example: simulate dice rolling we need a random integer between 1 and 6 answer: use modulus operator rand() % 6; generates a random integer between 0 and 5 rand() % 6 + 1; generates a random integer between 1 and 6 To generate a random integer between low and high : low + rand() % (high - low + 1)

9 Random numbers What if we want a random floating point number within some range? Step 1: map rand() to the 0  1 range rand() / RAND_MAX is between 0 and 1 rand() / (RAND_MAX+1) is between 0 and 1, not including 1 Step 2: scale the result to desired range similarly to how we did it for integers. To generate a floating point number between low and high : low + ( rand() / RAND_MAX ) % (high - low)

10 Random numbers Example: Simulating a die roll: Example: Simulating a coin toss: int roll; roll = rand()%6; cout << "You rolled a " << roll << ". Roll again? (y/n)"; int toss; if (rand()%2 == 0) cout << "Heads!"; else cout << "Tails!";