what I'm pretty sure I learned in school and what like every website said when I tried to look this up online was that the C++ code for generating a random number with floor: 1 and ceiling 100 (err I guess 101) is:

int number = rand() % 100 + 1;

and that is generating a number... but everytime I compile the program, or run the same program more than once, 'number' always = 8... like everytime...

and I tried changing it to

int number = rand() / 100 + 1;

but then number always = 169.. not really fixing the problem, just changing it.

what am I doing wrong? How do I generate a random number between 1 and 100 in C++ that is not the same every time?

[QUOTE=FALL3N;]yup, that did it, but just so I have a better feel for it, I seed the random number algorithm thing and then thats the first number the algorithm uses...[/quote]
No, the [I]seed[/I] designates where within the list of random numbers [ICODE]rand()[/ICODE] will start. Random numbers are not calculated, they …

All 8 Replies

You probably forgot to seed it. Try this:

#include <iostream>
#include <ctime>
using namespace std;

int main(){
srand( time(0) ); //seed the RGN
for(int i = 0; i < 10; ++i){
std::cout << (rand() % 100 + 1) << std::endl;
}
return 0;
}

You probably forgot to seed it.

In other words, you didn't use srand()

yup, that did it, but just so I have a better feel for it, I seed the random number algorithm thing and then thats the first number the algorithm uses...
a. Why was I getting 8 for every random number when I didn't seed the random number generator/what 'seed' was the generator using in the algorithm before?
b. what is time(0) ? is it the number of nanoseconds that have elapsed since the program was compiled or something? and what does the '0' parameter do?

yup, that did it, but just so I have a better feel for it, I seed the random number algorithm thing and then thats the first number the algorithm uses...

No, the seed designates where within the list of random numbers rand() will start. Random numbers are not calculated, they are pulled from a huge list.

a. Why was I getting 8 for every random number when I didn't seed the random number generator/what 'seed' was the generator using in the algorithm before?

Because the default seed always starts at the same place.

b. what is time(0) ? is it the number of nanoseconds that have elapsed since the program was compiled or something? and what does the '0' parameter do?

Look up the time() function. It has nothing to do with nanoseconds...

>>Random numbers are not calculated, they are pulled from a huge list

Are you sure? I thought they were calculated via linear congruential algorithm given a seed to start from.

Depends on the compiler I suppose...

Depends on the compiler I suppose...

Idk, maybe someone more knowledgeable can comment. I haven't seen one implementation that uses a list, simply because calculating at runtime is very fast and way less memory intensive than generating a list of pseudo random numbers, which can be very very long.

well, me understanding HOW it works was secondary to just having it work, which it does, so thanks again, everyone!