155k views
4 votes
Type two statements that use rand() to print 2 random integers between (and including) 100 and 149. End with a newline. Ex:

101
133
Note: For this activity, using one statement may yield different output (due to the compiler calling rand() in a different order). Use two statements for this activity. Also, srand() has already been called; do not call srand() again.

Code:

#include
#include // Enables use of rand()
#include // Enables use of time()
using namespace std;

int main() {
int seedVal = 0;

seedVal = 4;
srand(seedVal);

/* Your solution goes here */

return 0;
}

1 Answer

1 vote

Answer:

Replace the comment with:

cout<<100 + rand() % 49<<endl;

cout<<100 + rand() % 49<<endl;

Step-by-step explanation:

To generate random number from min to max, we make of the following syntax:

min + rand()%(max - min)

In this case:


min = 100


max = 149

So, we have:

100 + rand()%(149 - 100)

100 + rand()%49

To print the statements, make use of:

cout<<100 + rand() % 49<<endl;

cout<<100 + rand() % 49<<endl;

User Yemaw
by
5.7k points