Random Number Generator

Hi, I've scowered the forums for finding how too implement the random generator function inside of a class. I understand how it works inside the main function and how it runs.

What i am not familiar with is
1)how to 'seed' it in a class that will generator a random number.
2) how to return a random number without 'seeding' it every time in a function

I am using the method below in my main class.

1
2
3
  srand(time(0));
		for( int i = 0 ; i < 20; i ++)
			cout << (rand()%5);


prior to this post. I used to do it in the following form
1
2
3
4
srand(time(0));
int i =(rand()%5);

return i;



I am under the impression this is 'reseeding' every time which is wrong! But I can not find a solid solution :(


below is an example of how i am trying to implement this. But i am not sure where to seed the numGenerator
1
2
3
4
5
6
7
void abug::fillbox()
{
	for(int i = 0; i < 3; i++)
	{mybox[i] = randomBugs[(rand()%5)];
	cout << endl << mybox[i];
	}
}


Thanks folks.

Regards.

Losonnyboy

Last edited on
Call srand once at the beginning of main, and use rand wherever you want.
Topic archived. No new replies allowed.