1 Generating Random Numbers Textbook ch.6, pg. 356-367.

Slides:



Advertisements
Similar presentations
Savitch N/A Randomness In Programming Simulations
Advertisements

Tinaliah, S. Kom.. * * * * * * * * * * * * * * * * * #include using namespace std; void main () { for (int i = 1; i
Triana Elizabeth, S.Kom. #include using namespace std; void main () { for (int i = 1; i
1 Random number generation Using srand(), rand() & time(0) Searching and Sorting Demo Making searching & sorting more generic Overloading the functions.
Programming Functions: Passing Parameters by Reference.
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:
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 5 Function Basics.
CS Sept Your first C++ program… Boilerplate // Cannon, demo program #include using namespace std; int main() {// program goes here… return.
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 Session-23 CSIT 121 Spring 2006 Revision Ch 7: Reference Parameters Revision Ch 7: Reference Parameters Chapter 8: Value Returning Functions Chapter.
© 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.
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.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
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,
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.
 Monday 10/18/2010  Content: Week 1 – Week 6  Format:  Multiple choice questions  Matching questions  Determine what’s wrong  Determine the results.
 2006 Pearson Education, Inc. All rights reserved Functions and an Introduction to Recursion.
Chapter 3 Expressions and Interactivity Department of Computer Science Missouri State Univeristy.
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.
CSIS 113A Lecture 5 Random Numbers, while, do-while.
 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
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
Random numbers in C++ Nobody knows what’s next....
Variables and memory addresses
CSci 162 Lecture 7 Martin van Bommel. Random Numbers Until now, all programs have behaved deterministically - completely predictable and repeatable based.
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.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
Review. Problem 1 What is the output of the following piece of code void increment(int x) { x++; } int main() { int y = 10; increment(y); cout
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 4 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.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Functions and Libraries. Reference parameters void setToZero(int var) { var = 0; } int main() { int var = 0; setToZero(var); cout
MR. CRONE Generating Random Numbers. Random Numbers Many programs require the computer to generate random numbers Random numbers are used in many applications.
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.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
CISC181 Introduction to Computer Science Dr
Why they aren't really random
Functions and an Introduction to Recursion
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
Controlling execution - iteration
Iterative Constructs Review
Chapter 5 Function Basics
CSC113: Computer Programming (Theory = 03, Lab = 01)
Lab Session-9 CSIT-121 Spring 2005
توابع در C++ قسمت اول اصول كامپيوتر 1.
CSC1201: Programming Language 2
Random Number Generation
Pointers & Functions.
CS150 Introduction to Computer Science 1
CSC1201: Programming Language 2
CS 144 Advanced C++ Programming January 31 Class Meeting
Pointers & Functions.
6 Functions.
Presentation transcript:

1 Generating Random Numbers Textbook ch.6, pg

2 Random Number Generation rand function (need to include )  i = rand();  Generates unsigned integer between 0 and RAND_MAX (usually 32767) Scaling and shifting  Examples i = rand() % 6; generates a number between 0 and 5 (scaling) i = 1 + rand() % 6; generates a number between 1 and 6 (the “+ 1” makes the shift by 1)

3 Example Program #include using namespace std; void main() { int i; cout<<"Generating 10 random integers in the range 0- 5:"<<endl; for (i=0; i<10 ;i++) cout<<" "<<rand()%6<<" "; cout<<endl; cout<<"Generating 10 random integers in the range 1- 6:"<<endl; for (i=0; i<10 ;i++) cout<<" "<<1+rand()%6<<" "; cout<<endl; }

4 Generating 10 random integers in the range 0-5: Generating 10 random integers in the range 1-6: Example’s Program Output

5 General shifting and scaling –Number = shiftingValue + rand() % scalingFactor –shiftingValue = first number in desired range –scalingFactor = width of desired range Shifting and Scaling

6 Calling rand() repeatedly  Gives the same sequence of numbers Pseudorandom numbers  Preset sequence of "random" numbers  Same sequence generated whenever program runs To get different random sequences  Provide a seed value (unsigned integer value) Like a random starting point in the sequence The same seed will give the same sequence  srand(seed); Used before rand() to set the seed Pseudorandom Numbers

7 OR can use the current time to set the seed  No need to explicitly set seed every time  srand( time( 0 ) );  The time function returns the current time in seconds (need to include )  Call the above statement ONCE in the beginning of the main program, before you call the rand() function. Pseudorandom Numbers (ctd’)

8 Example Program #include using namespace std; void main() { int i; srand(time(0)); //this generates the first seed value cout<<“Generating 10 random integers in the range 0-5 using time as seed:"<<endl; for (i=0; i<10 ;i++) cout<<" "<<rand()%6<<" "; cout<<endl; }

9 Generating 10 random integers in the range 0-5 using time as seed: Example’s Program Output