Generating random numbers between two numbers

I have started this program which is supposed to generate 10 random numbers between 1 and 100 (inclusive). Right now, the program is generating 10 random numbers but they are not within 1 and 100. By the way, I am not allowed to use srand. Any tips?
Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;

/*Write a program which returns 10 random numbers between 1 and 100.
Use the "rand" function and the % operator.
*/

int main ()

{
	
	for (int nCount = 0; nCount < 10; ++nCount) //print 10 numbers
	{
		cout << rand() << endl;
		
	} while (rand() % 100 +1); //numbers must be between 1 and 100 (inclusive)
	
}
Okay, what the...

How about
cout << rand()%100+1 << endl;

and what on earth is the (never-ending) while loop supposed to accomplish?
Values returned by rand in line 15 and in line 17 have no connection. The right way would be cout << rand() % 100 + 1. Also, while loop in your code has no effect on anything. It starts after for loop and has no statements.
Okay, thanks for your help... got it working correctly.
while (rand() % 100 +1);

This line checks the condition for true or false, which is probably not what you think it did.

in pseudo-code:

1
2
3
4
while ((rand() & 100 +1) == true)
do nothing;

//end of program 


That's what it does, I believe, and from what you described your assignment to be, that's not it...

you need to cout the line while (rand() % 100 +1); inside the for loop, so that it actually passes some values to the rand() function each loop.


Edit: Ninja'd! :D
Last edited on
Topic archived. No new replies allowed.