Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
6.7 Case Study: Random Number Generation 1 • C++ Standard Library function rand – Introduces the element of chance into computer applications – Example • i = rand(); – Generates an unsigned integer between 0 and RAND_MAX (a symbolic constant defined in header file <cstdlib>) – Function prototype for the rand function is in <cstdlib> 2008 Pearson Education, Inc. All rights reserved. 6.7 Case Study: Random Number Generation (Cont.) 2 • To produce integers in a specific range, use the modulus operator (%) with rand – Example rand() % 6; – Produces numbers in the range 0 to 5 – This is called scaling, 6 is the scaling factor – Shifting can move the range to 1 to 6 1 + rand() % 6; 2008 Pearson Education, Inc. All rights reserved. 1 // Fig. 6.8: fig06_08.cpp 2 // Shifted and scaled random integers. 3 #include <iostream> 4 using std::cout; 5 using std::endl; 3 Outline fig06_08.cpp 6 7 #include <iomanip> 8 using std::setw; (1 of 2) 9 10 #include <cstdlib> // contains function prototype for rand 11 using std::rand; 12 #include and using for function rand 13 int main() 14 { 15 // loop 20 times 16 for ( int counter = 1; counter <= 20; counter++ ) 17 { 18 // pick random number from 1 to 6 and output it 19 cout << setw( 10 ) << ( 1 + rand() % 6 ); Calling function rand 2008 Pearson Education, Inc. All rights reserved. 20 4 21 // if counter is divisible by 5, start a new line of output 22 if ( counter % 5 == 0 ) cout << endl; 23 24 } // end for fig06_08.cpp 25 26 Outline return 0; // indicates successful termination (2 of 2) 27 } // end main 6 5 6 6 6 1 6 2 5 1 2 3 5 5 4 4 6 3 2 1 2008 Pearson Education, Inc. All rights reserved. 6.7 Case Study: Random Number Generation (Cont.) 5 • Function rand – Generates pseudorandom numbers – The same sequence of numbers repeats itself each time the program executes • Randomizing – Conditioning a program to produce a different sequence of random numbers for each execution • C++ Standard Library function srand – Takes an unsigned integer argument – Seeds the rand function to produce a different sequence of random numbers 2008 Pearson Education, Inc. All rights reserved. 1 // Fig. 6.10: fig06_10.cpp 2 // Randomizing die-rolling program. 3 #include <iostream> 4 using std::cout; 5 using std::cin; 6 using std::endl; fig06_10.cpp 8 #include <iomanip> (1 of 2) 9 using std::setw; 6 Outline 7 10 11 #include <cstdlib> // contains prototypes for functions srand and rand 12 using std::rand; 13 using std::srand; 14 using statement for function srand 15 int main() 16 { 17 unsigned seed; // stores the seed entered by the user 18 Data type unsigned is short for unsigned int 19 cout << "Enter seed: "; 20 cin >> seed; 21 srand( seed ); // seed random number generator 22 Passing seed to srand to randomize the program 2008 Pearson Education, Inc. All rights reserved. 23 // loop 10 times 24 25 26 for ( int counter = 1; counter <= 10; counter++ ) { // pick random number from 1 to 6 and output it 7 Outline 27 28 cout << setw( 10 ) << ( 1 + rand() % 6 ); 29 30 // if counter is divisible by 5, start a new line of output if ( counter % 5 == 0 ) 31 32 cout << endl; } // end for fig06_10.cpp (2 of 2) 33 34 return 0; // indicates successful termination 35 } // end main Enter seed: 67 6 1 1 6 4 1 6 6 2 4 Enter seed: 432 4 3 6 1 3 5 1 4 6 2 Enter seed: 67 6 1 1 6 4 1 6 6 2 4 Program outputs show that each unique seed value produces a different sequence of random numbers 2008 Pearson Education, Inc. All rights reserved. 6.7 Case Study: Random Number Generation (Cont.) 8 • To randomize without having to enter a seed each time srand( time( 0 ) ); • This causes the computer to read its clock to obtain the seed value – Function time (with the argument 0) • Returns the current time as the number of seconds since January 1, 1970 at midnight Greenwich Mean Time (GMT) • Function prototype for time is in <ctime> 2008 Pearson Education, Inc. All rights reserved. 6.7 Case Study: Random Number Generation (Cont.) 9 • Scaling and shifting random numbers – To obtain random numbers in a desired range, use a statement like number = shiftingValue + rand() % scalingFactor; • shiftingValue is equal to the first number in the desired range of consecutive integers • scalingFactor is equal to the width of the desired range of consecutive integers – number of consecutive integers in the range 2008 Pearson Education, Inc. All rights reserved. 1 // Fig. 7.10: fig07_10.cpp 2 // Roll a six-sided die 6,000,000 times. 3 4 #include <iostream> using std::cout; 5 using std::endl; 6 7 8 #include <iomanip> using std::setw; 10 Outline fig07_10.cpp (1 of 2) 9 10 #include <cstdlib> 11 using std::rand; 12 using std::srand; 13 14 #include <ctime> 15 using std::time; 16 17 int main() 18 { Declare frequency as array of 7 ints 19 20 21 const int arraySize = 7; // ignore element zero int frequency[ arraySize ] = { 0 }; 22 23 srand( time( 0 ) ); // seed random number generator 24 25 26 // roll die 6,000,000 times; use die value as frequency index for ( int roll = 1; roll <= 6000000; roll++ ) frequency[ 1 + rand() % 6 ]++; Generate 6000000 random integers in range 1 to 6 Increment frequency values at the index associated with the random number 2008 Pearson Education, Inc. All rights reserved. 27 28 11 Outline cout << "Face" << setw( 13 ) << "Frequency" << endl; 29 30 // output each array element's value 31 for ( int face = 1; face < arraySize; face++ ) 32 33 cout << setw( 4 ) << face << setw( 13 ) << frequency[ face ] << endl; (2 of 2) 34 35 fig07_10.cpp return 0; // indicates successful termination 36 } // end main Face 1 2 3 4 5 6 Frequency 1000167 1000149 1000152 998748 999626 1001158 2008 Pearson Education, Inc. All rights reserved.