In: Computer Science
For this assignment, write a program that will generate three randomly sized sets of random numbers using DEV C++
To use the random number generator, first add a #include statement for the cstdlib library to the top of the program:
#include <cstdlib>
Next, initialize the random number generator. This is done by calling the srand function and passing in an integer value (known as a seed value). This should only be done ONE time and it MUST be done before actually getting a random number. A value of 1 (or any integer literal) will generate the same sequence of "random" numbers every time the program is executed. This can be useful for debugging:
srand(1);
To get a different series of random numbers each time the program is run, the actual time that the program is run can be passed as the seed value for the random number generator. This is done as follows:
srand(time(0));
If the time function is used, make sure to include the ctime library as well.
Note: the two srand instructions that are listed above are simple examples of how to use the instruction. In a program, only one version will be used.
Now that the random number generator has been initialized, a random number can be generated by calling the rand function:
num = rand();
The above line of C++ code will generate a "random" integer between 0 and RAND_MAX and saves the value in an integer variable named num. RAND_MAX is a pre-defined constant that is equal to the maximum possible random number. It is implementation dependent but is guaranteed to be at least 32,767.
Modulus division can be used to restrict the "random" integer to a smaller range:
num = rand() % 7;
will produce a value between 0 and 6. To change the range to 1 through 7, simply add 1:
num = rand() % 7 + 1;
To get random values that are within a specified range that starts at a value other than 0 or 1:
num = minimum_value + (rand() % (maximum_value - minimum_value + 1));
So, to get values within the range 3 - 17:
num = 3 + (rand() % (17 - 3 + 1));
Run 1 (using srand(5);) on Windows PC
There are 59 numbers in the first set of numbers. 93 55 49 60 30 27 49 72 40 14 21 33 76 26 7 63 7 50 31 17 92 93 11 36 49 52 83 22 31 51 69 59 10 53 15 22 87 83 34 86 6 54 85 15 19 60 15 46 12 84 5 91 59 33 99 70 4 17 36 There are 235 numbers in the second set of numbers. 66 38 1 36 10 89 90 93 51 6 35 50 68 46 82 75 35 82 60 53 40 9 53 85 90 16 39 93 63 85 86 84 17 58 78 60 19 67 85 0 26 71 80 74 78 85 43 73 33 29 39 56 61 75 92 83 55 86 19 66 70 86 21 75 46 58 72 2 51 47 82 16 17 91 16 68 41 25 9 86 51 33 67 89 61 46 73 82 24 91 49 43 54 27 32 72 76 96 16 97 97 5 73 27 58 86 52 68 7 68 59 61 98 2 25 86 75 16 93 89 32 82 68 74 21 71 20 67 94 58 30 70 0 72 24 95 86 8 87 36 77 71 14 26 46 8 76 2 50 55 19 24 46 16 34 71 33 71 60 25 58 5 93 11 86 34 72 32 33 80 42 30 0 10 38 58 67 98 45 26 24 24 28 84 36 17 0 4 60 95 69 60 55 69 42 40 26 93 32 53 0 28 64 74 75 17 30 72 30 54 48 37 8 39 4 44 65 81 5 43 28 98 67 63 69 14 68 63 80 73 89 58 17 82 22 There are 205 numbers in the third set of values. 81 40 35 33 69 58 56 79 66 0 2 24 65 35 50 84 7 26 85 35 88 75 24 58 16 20 38 23 18 7 44 52 16 82 36 47 22 31 30 21 78 59 54 88 0 17 90 81 87 73 59 58 60 94 49 92 22 29 81 1 97 39 49 71 59 32 90 36 55 33 25 97 40 23 34 81 66 29 38 88 35 88 2 55 5 45 44 94 34 83 26 91 16 85 10 64 1 66 28 96 66 87 18 34 60 53 83 90 23 12 65 84 71 75 98 31 35 5 29 22 72 51 22 37 38 51 62 26 56 12 23 1 22 27 76 85 34 61 92 48 68 42 32 78 95 54 6 32 67 26 51 62 36 25 93 59 54 51 25 45 15 54 55 73 19 51 24 36 2 79 19 97 23 66 91 5 91 1 27 20 47 55 15 62 42 13 70 94 58 98 17 6 18 23 75 11 52 28 45 30 89 95 32 95 49
If you have any doubts, please give me comment...
#include<iostream>
#include<cstdlib>
using namespace std;
void generateRandom(int n);
int main(){
srand(5);
generateRandom(59);
generateRandom(235);
generateRandom(205);
return 0;
}
void generateRandom(int n){
int num;
cout<<"There are "<<n<<" numbers in the first set of numbers."<<endl<<endl;
for(int i=1; i<=n; i++){
num = rand()%100;
cout<<num;
if(i%20==0)
cout<<endl;
else
cout<<" ";
}
cout<<endl<<endl;
}