100 likes | 205 Views
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.
E N D
rand() #include <iostream> using namespace std; int main() { for(inti = 1; i <= 10; i++) cout << rand() << endl; return 0; }
rand() First Execution 3845773388773849487724339944523740082314611833432237844 #include <iostream> using namespace std; int main() { for(inti = 1; i <= 10; i++) cout << rand() << endl; return 0; }
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; }
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; }
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; }
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; }
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; }