DOC PREVIEW
UD CISC 181 - Random Number Generation

This preview shows page 1-2-3-23-24-25-26-46-47-48 out of 48 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 September 22, 20093.8 Random Number Generationfig03_07.cpp (1 of 2)fig03_07.cpp (2 of 2) fig03_07.cpp output (1 of 1)Example ProgramSlide 6fig03_09.cpp (1 of 2)fig03_09.cpp (2 of 2) fig03_09.cpp output (1 of 1)Slide 9Example using Seed3.9 Example: Game of Chance and Introducing enumSlide 12fig03_10.cpp (1 of 5)fig03_10.cpp (2 of 5)fig03_10.cpp (3 of 5)fig03_10.cpp (4 of 5)fig03_10.cpp (5 of 5) fig03_10.cpp output (1 of 2)fig03_10.cpp output (2 of 2)Scope Rules3.11 Scope RulesSlide 21Block Scope3.10 Storage ClassesSlide 24Slide 25fig03_12.cpp (1 of 5)fig03_12.cpp (2 of 5)fig03_12.cpp (3 of 5)fig03_12.cpp (4 of 5)fig03_12.cpp (5 of 5) fig03_12.cpp output (1 of 2)fig03_12.cpp output (2 of 2)ParametersCall-by-Value ParametersCall-by-Value Example: Display 4.1 Formal Parameter Used as a Local Variable (1 of 3)Call-by-Value Example: Display 4.1 Formal Parameter Used as a Local Variable (2 of 3)Slide 36Call-by-Value Example: Display 4.1 Formal Parameter Used as a Local Variable (3 of 3)Call-by-Value Pitfall3.17 References and Reference ParametersSlide 40fig03_20.cpp (1 of 2)fig03_20.cpp (2 of 2)fig03_20.cpp output (1 of 1)Slide 44Call-By-Reference DetailsConstant Reference Parametersfig03_21.cpp (1 of 1) fig03_21.cpp output (1 of 1)fig03_22.cpp (1 of 1) fig03_22.cpp output (1 of 1)1CISC181 Introduction to Computer ScienceDr. McCoyLecture 7September 22, 2009 2003 Prentice Hall, Inc. All rights reserved.23.8 Random Number Generation•rand function (<cstdlib>)–i = rand();–Generates unsigned integer between 0 and RAND_MAX (usually 32767)•Scaling and shifting–Modulus (remainder) operator: %•10 % 3 is 1•x % y is between 0 and y – 1–Examplei = rand() % 6 + 1;•“Rand() % 6” generates a number between 0 and 5 (scaling)•“+ 1” makes the range 1 to 6 (shift)–Next: program to roll dice 2003 Prentice Hall, Inc.All rights reserved.Outline3fig03_07.cpp(1 of 2)1 // Fig. 3.7: fig03_07.cpp2 // Shifted, scaled integers produced by 1 + rand() % 6.3 #include <iostream>4 5 using std::cout;6 using std::endl;7 8 #include <iomanip>9 10 using std::setw;11 12 #include <cstdlib> // contains function prototype for rand 13 14 int main()15 {16 // loop 20 times17 for ( int counter = 1; counter <= 20; counter++ ) {18 19 // pick random number from 1 to 6 and output it20 cout << setw( 10 ) << ( 1 + rand() % 6 );21 22 // if counter divisible by 5, begin new line of output23 if ( counter % 5 == 0 )24 cout << endl;25 26 } // end for structureOutput of rand() scaled and shifted to be a number between 1 and 6. 2003 Prentice Hall, Inc.All rights reserved.Outline4fig03_07.cpp(2 of 2)fig03_07.cppoutput (1 of 1)27 28 return 0; // indicates successful termination29 30 } // end main6 6 5 5 6 5 1 1 5 3 6 6 2 4 2 6 2 3 4 15Example Program•A program that generates a random number between 7-10 (i.e., 7, 8, 9, 10)•What is the statement that generates in that range?•Generate a large number (say 7000) and count how many times each number is generated.•What is the structure of that program?•~mccoy/Class/cisc181/examples/rand-num-7-10.cc 2003 Prentice Hall, Inc. All rights reserved.63.8 Random Number Generation•Calling rand() repeatedly –Gives the same sequence of numbers•Pseudorandom numbers–Preset sequence of "random" numbers–Same sequence generated whenever program run•To get different random sequences–Provide a seed value•Like a random starting point in the sequence•The same seed will give the same sequence–srand(seed); •<cstdlib>•Used before rand() to set the seed 2003 Prentice Hall, Inc.All rights reserved.Outline7fig03_09.cpp(1 of 2)1 // Fig. 3.9: fig03_09.cpp2 // Randomizing die-rolling program.3 #include <iostream>4 5 using std::cout;6 using std::cin;7 using std::endl;8 9 #include <iomanip>10 11 using std::setw;12 13 // contains prototypes for functions srand and rand14 #include <cstdlib>15 16 // main function begins program execution17 int main()18 {19 unsigned seed;20 21 cout << "Enter seed: ";22 cin >> seed;23 srand( seed ); // seed random number generator24 Setting the seed with srand(). 2003 Prentice Hall, Inc.All rights reserved.Outline8fig03_09.cpp(2 of 2)fig03_09.cppoutput (1 of 1)25 // loop 10 times26 for ( int counter = 1; counter <= 10; counter++ ) {27 28 // pick random number from 1 to 6 and output it29 cout << setw( 10 ) << ( 1 + rand() % 6 );30 31 // if counter divisible by 5, begin new line of output32 if ( counter % 5 == 0 )33 cout << endl;34 35 } // end for 36 37 return 0; // indicates successful termination38 39 } // end mainEnter seed: 67 6 1 4 6 2 1 6 1 6 4Enter seed: 432 4 6 3 1 6 3 1 5 4 2Enter seed: 67 6 1 4 6 2 1 6 1 6 4rand() gives the same sequence if it has the same initial seed. 2003 Prentice Hall, Inc. All rights reserved.93.8 Random Number Generation•Can use the current time to set the seed–No need to explicitly set seed every time–srand( time( 0 ) );–time( 0 );•<ctime>•Returns current time in seconds•General shifting and scaling–Number = shiftingValue + rand() % scalingFactor–shiftingValue = first number in desired range–scalingFactor = width of desired range10Example using Seed•~mccoy/Class/cisc181/examples/rand-num-seed-7-10.cc•Very same program as before, except a seed is done at beginning – so each time it is run the sequence of random numbers is different. 2003 Prentice Hall, Inc. All rights reserved.113.9 Example: Game of Chance and Introducing enum•Enumeration–Set of integers with identifiersenum typeName {constant1, constant2…};–Constants start at 0 (default), incremented by 1–Constants need unique names–Cannot assign integer to enumeration variable•Must use a previously defined enumeration type•Exampleenum


View Full Document
Download Random Number Generation
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Random Number Generation and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Random Number Generation 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?