सबसे पहले हम रैंड () फ़ंक्शन के बारे में चर्चा करते हैं। रैंड () फ़ंक्शन C ++ की एक पूर्वनिर्धारित विधि है। इसे
एल्गोरिदम
Begin Declare max_n to the integer datatype. Initialize max_n = 100. Declare min_n to the integer datatype. Initialize min_n = 1. Declare new_n to the integer datatype. Declare i of integer datatype. Print “The random number is:”. for (i = 0; i < 10; i++) new_n = ((rand() % (max_n + 1 - min_n)) + min_n) Print the value of new_n. End.
उदाहरण
#include <iostream>
#include <stdlib.h>
using namespace std;
int main() {
int max_n = 100;
int min_n = 1;
int new_n;
int i;
cout<<"The random number is: \n";
for (i = 0; i < 10; i++) {
new_n = ((rand() % (max_n + 1 - min_n)) + min_n);
//rand() returns random decimal number.
cout<<new_n<<endl;
}
return 0;
} आउटपुट
The random number is: 42 68 35 1 70 25 79 59 63 65