1 / 13

Random Number Generator

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- srand. algorithmic (software) generators. • Algorithmic generators are widely accepted because they meet all of the following

Download Presentation

Random Number Generator

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 <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 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 return0; // 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

  9. 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)

  10. Srand(NULL)function

  11. Srand(NULL)function • Advantage: • Every time generate random number without intervention from the user

More Related