Download presentation
Presentation is loading. Please wait.
Published byErick Stone Modified over 9 years ago
1
Random Number Generator
2
Using Software We can generate random number by: 1- table 2- hardware 3-- software Function to generate RN by SW is: 1-rand 2- srand
3
algorithmic (software) generators Algorithmic generators are widely accepted because they meet all of the following criteria. randomness - output passes all reasonable statistical tests of randomness controllability - able to reproduce output, if desired portability - able to produce the same output on a wide variety of computer systems efficiency - fast, minimal computer resource requirements documentation - theoretically analyzed and extensively tested
4
Rand function It’s advantage that it produce the same number. They solve this drawback by using (srand) function
5
Code by (rand) function 1 2 // Shifted, scaled integers produced by 1 + rand() % 6. 3 #include 4 5 using std::cout; 6 using std::endl; 7 8 #include 9 10 using std::setw; 11 12 #include // contains function prototype for rand 13 14 int main() 15 { 16 // loop 20 times 17 for ( int counter = 1; counter <= 20; counter++ ) { 18 19 // pick random number from 1 to 6 and output it 20 cout << setw( 10 ) << ( 1 + rand() % 6 ); 21 22 // if counter divisible by 5, begin new line of output 23 if ( counter % 5 == 0 ) 24 cout << endl; 25 26 } // end for structure
6
27 28 return 0; // indicates successful termination 29 30 } // end main 6 6 5 5 6 5 1 1 5 3 6 6 2 4 2 6 2 3 4 1
7
(Srand) function It used to solve the disadvantage of (rand) function Advantage of (srand) is: 1- every time it run it produce different number The disadvantage of (srand) is: 1- the user must enter the variable to start generator.
8
Code by (srand) function
10
Develop to use (srand) function To prevent the user from entering the variable they make enhancement to (srand) by using (srand(time(0))) To use this function we must add the library (time.h)
11
Srand(NULL)function
13
Advantage: Every time generate random number without intervention from the user
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.