Question

In: Computer Science

For this assignment, write a program that will generate three randomly sized sets of random numbers...

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

Solutions

Expert Solution

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;

}


Related Solutions

Write a C++ program to generate two random numbers (Rnd1 and Rnd2). These two numbers should...
Write a C++ program to generate two random numbers (Rnd1 and Rnd2). These two numbers should be within a range [100, 500]. If Rnd1 greater than or equals to Rnd2, print out the result of (Rnd1-Rnd2). Otherwise, print out the result of (Rnd2-Rnd1). In all cases, print Rnd1, Rnd2, and the results of subtractions in suitable messages.
Write a Java program to generate random numbers in the following range a. 1 <=n <=...
Write a Java program to generate random numbers in the following range a. 1 <=n <= 3 b. 1 <= n <= 200 c. 0 <= n <= 9 d. 1000 <= n <= 2112 e. -1 <= n <= 5 JAVA PROGRAMMING
java please 1. Write a Java program to generate random numbers in the following range a....
java please 1. Write a Java program to generate random numbers in the following range a. 1 <=n <= 3 b. 1 <= n <= 200 c. 0 <= n <= 9 d. 1000 <= n <= 2112 e. -1 <= n <= 5 2. Write statements that assign random integers to the variable n in the following ranges: a) 1 ≤ n ≤ 2 b) 1 ≤ n ≤ 100 c) 0 ≤ n ≤ 9 d) 1000 ≤...
Write a C program that prompts the user to enter three sets of five double numbers...
Write a C program that prompts the user to enter three sets of five double numbers each. (You may assume the user responds correctly and doesn’t enter non-numeric data.) The program should accomplish all of the following: a. Store the information in a 3×5 array. b. Compute the average of each set of five values. c. Compute the average of all the values. d. Determine the largest value of the 15 values. e. Report the results. Each major task should...
Write a program that prompts the user to enter three sets of five double numbers each....
Write a program that prompts the user to enter three sets of five double numbers each. (You may assume the user responds correctly and doesn’t enter non-numeric data.) The program should accomplish all of the following: a. Store the information in a 3×5 array. b. Compute the average of each set of five values. c. Compute the average of all the values. d. Determine the largest value of the 15 values. e. Report the results. Each major task should be...
Write a function that will generate an array of random numbers. It needs to:
DO IN C++Write a function that will generate an array of random numbers. It needs to:Take 3 integers as parameters-The first is the minimum value-the second is the maximum value-the third is the size of the new array-create a dynamically allocated array of the correct size-generate a random number (between min and max) for each element in the array-return a pointer to the arrayCreate a main() function that tests the above function and displays the values in the random array.
For this problem, you will write a program using two queues. Generate n random numbers between...
For this problem, you will write a program using two queues. Generate n random numbers between 10 and 100 (both inclusive), where n>9. The value of n should be taken as input from the user and n should be >9. The numbers could be duplicated. Enqueue all these numbers to the first queue. The objective is to find the numbers whose sum of digits is odd and enqueue them to the second queue. The remaining numbers (whose sum of digits...
Write a program to simulate a calculator. Generate two random numbers (between 1-15) and an ask...
Write a program to simulate a calculator. Generate two random numbers (between 1-15) and an ask the user for an arithmetic operator. Using a switch statement, your program has to perform the arithmetic operation on those two operands depending on what operator the user entered. Ask the user if he/she wants to repeat the calculations and if the answer is yes or YES, keep repeating. If the answer is something else, then stop after the first calculation. c++
Write the R code to generate five independent uniform random numbers and use that to generate...
Write the R code to generate five independent uniform random numbers and use that to generate 5 independent Bernoulli random variables with probability of success p = 0.4. please use RStudio. please do not use lab nor rbern for calculations.
Write an Arduino code that does the following. Generate 50 random numbers between the numbers 100...
Write an Arduino code that does the following. Generate 50 random numbers between the numbers 100 and 300. Pick a number at random out of these 50 random variables. a. Determine the probability of the chosen number being greater than 200. This may be achieved by counting the numbers that are greater than 200 and dividing the count by 50. Make sure you, i.Formulate the appropriate if-conditions to check for a number being greater than 200 ii. Use a for-loop...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT