1 / 10

Random Number Generation

Random Number Generation. Pseudo-Random Generation. rand(). #include < iostream > using namespace std; int main() {   for( int i = 1; i <= 10; i ++) cout << rand() << endl ; return 0; }. rand(). First Execution.

Download Presentation

Random Number Generation

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 Generation

  2. Pseudo-Random Generation

  3. rand() #include <iostream> using namespace std; int main() {   for(inti = 1; i <= 10; i++) cout << rand() << endl; return 0; }

  4. rand() First Execution 3845773388773849487724339944523740082314611833432237844 #include <iostream> using namespace std; int main() {   for(inti = 1; i <= 10; i++) cout << rand() << endl; return 0; }

  5. rand() First Execution Second Execution 3845773388773849487724339944523740082314611833432237844 3845773388773849487724339944523740082314611833432237844 #include <iostream> using namespace std; int main() {   for(inti = 1; i <= 10; i++) cout << rand() << endl; return 0; }

  6. srand() #include <iostream> #include <ctime> #include <cstdlib> using namespace std; int main() { srand(time(NULL));   for(inti = 1; i <= 10; i++) cout << rand() << endl; return 0; }

  7. srand() First Execution 46453735342236578889937237165742245778614 #include <iostream> #include <ctime> #include <cstdlib> using namespace std; int main() { srand(time(NULL));   for(inti = 1; i <= 10; i++) cout << rand() << endl; return 0; }

  8. srand() First Execution Second Execution 46453735342236578889937237165742245778614 6877245768215576187851179738346117511735257868 #include <iostream> #include <ctime> #include <cstdlib> using namespace std; int main() { srand(time(NULL));   for(inti = 1; i <= 10; i++) cout << rand() << endl; return 0; }

  9. limiting range: [0, MAX_RAND] #include <iostream> #include <ctime> #include <cstdlib> using namespace std; int main() {   long next_value; srand(time(NULL));   for (inti = 1; i <= 100000; i++)     if (rand()%100 <= 34) cout << "hello" << endl; // ~35,000 times     else cout << "goodbye" << endl; // ~65,000 times   return 0; }

  10. End of Session

More Related