Download presentation
Presentation is loading. Please wait.
1
Random Number Generation
2
Pseudo-Random Generation
3
rand() #include <iostream> using namespace std; int main() { for(int i = 1; i <= 10; i++) cout << rand() << endl; return 0; }
4
rand() First Execution #include <iostream> using namespace std; int main() { for(int i = 1; i <= 10; i++) cout << rand() << endl; return 0; }
5
rand() First Execution Second Execution #include <iostream> using namespace std; int main() { for(int i = 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(int i = 1; i <= 10; i++) cout << rand() << endl; return 0; }
7
srand() First Execution #include <iostream> #include <ctime> #include <cstdlib> using namespace std; int main() { srand(time(NULL)); for(int i = 1; i <= 10; i++) cout << rand() << endl; return 0; }
8
srand() First Execution Second Execution #include <iostream> #include <ctime> #include <cstdlib> using namespace std; int main() { srand(time(NULL)); for(int i = 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 (int i = 1; i <= ; i++) if (rand()%100 <= 34) cout << "hello" << endl; // ~35,000 times else cout << "goodbye" << endl; // ~65,000 times return 0; }
10
End of Session
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.