Public Group

# random number

This topic is 4900 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hey there, im trying to generate 3 random numbers that must be different at the same time first i used the code int a = rand()%10+1; this game me 3 random numbers but i realised that they gave the same 3 numbers every time i executed the program exe1 214, 453, 222, 666 exe2 214, 453, 222, 666 . . . not very random, cos the same random numbers are coming in each time next i tried srand(time(NULL)); int a = rand()%10+1; this gave me totally different numbers on exe, but they are all the same eg 222, 666, 999 so, say i put the rndnum in a class so i can call int a = gennum.gen(); int b = gennum.gen() int c = gennum.gen() how do i make it so they are all different numbers? thanks

##### Share on other sites
Pure random numbers doesn't exist in the world of computers. Random numbers are calculated using a specific algorithm, and are thuse semi-random.

You would either have to change to a better library or cope with it. Where are you calling srand()? Because calling it multiple times messes up the seed.

Call srand() at the start of your program, and calling rand() throughout the rest of your application shouldn't give you problems then. At least, I never had problems with it.

Toolmaker

##### Share on other sites
yup. srand once, at the start. In terms of a random class, in the constructor:

class Random{    Random() { srand(time(0)); }    operator()(int range) { return Rand()%range + 1; }}Random r;int main(){    cout << r() << endl << r() << endl << r() << endl;}

The added advantage of this is that you can have separate independant streams of random numbers, which can be very important, but probably isn't important to you _yet_.

##### Share on other sites
Quote:
 Original post by Squirmyup. srand once, at the start. In terms of a random class, in the constructor:class Random{ Random() { srand(time(0)); } operator()(int range) { return Rand()%range + 1; }}Random r;int main(){ cout << r() << endl << r() << endl << r() << endl;}The added advantage of this is that you can have separate independant streams of random numbers, which can be very important, but probably isn't important to you _yet_.

That will not result in independent streams of random numbers, as every instance of the Random class uses the same pseudo-random number source. Concider this example.
	Random r1;	Random r2;	cout << r1() << r2() << r1() << r2() << endl;	Random r3;	cout << r3() << r3() << r3() << r3() << endl;

If the program runs fast enough for the timer to return the same time in all three constructors (or if you just manually pick an arbitrary seed), the r1/r2 combination will generate the same number as r3 alone. Had they been independent sources (assuming they get seeded with the same number), r1 and r2 would generate the same numbers serie in the first line, but they don't.

As deterministic random sources (deterministic source as in you can repeat the sequence using the same seed), they are not independent. As non-deterministic random sources (as in repeatability for a given seed not a necessary feature), it's useless to implement it as a class anyway, as a single function would work just as well.

##### Share on other sites
ok my rand class

int CRandomNumber::GenerateNumber()
{

srand(time(NULL));
int rnd = rand()%10+1;
return rnd;

}

even in main if i go

CRandomNumber rnd1
CRandomNumber rnd2
CRandomNumber rnd3

i still get the same number 3 times . . . any m ore suggestions

##### Share on other sites
As said by many people already, you should only call srand once. The code you show above calls it for every random number you generate.

##### Share on other sites
You need to srand in your constructor, not your get random function.

My apologies for my version without thinking - Im not used to using srand and rand anymore - you are right that the way I've written it you should only instantiate one instance of the class, so if you need random numbers in a lot of places a global variable or possibly even (hello washu) a singleton would be a good move:

class Random{public:    static Random & Instance() { static Random r; return r; }    int Int(int range) { return rand()%range + 1; }    float Float() { return float(rand())/float(MAX_RAND); }private:    Random() { srand(time(0)); }    Random(const Random &);    operator=(const Random &);}int main{    cout << Random::Instance().Int() << Random::Instance().Float << endl;}

The earlier poster is also right in saying that this doesn't need to be a class - it doesnt have much in the way of state - so it can be written as follows:

int RandInt(int range) { SeedFirst(); return rand()%range + 1; }float RandFloat() { SeedFirst(); return float(rand())/float(MAX_RAND); }void SeedFirst() { static bool first = true; if(first) srand(time(0)); first = false; }

##### Share on other sites
ah yes, thanks, great help . . . but i promise, there will be more stupid questons to come

##### Share on other sites
I'm a noob so sorry if this is a stupid question. But if you put rand in a loop and loop it 3 times wouldn't that provide 3 different numbers?

##### Share on other sites
Quote:
 Original post by LacanI'm a noob so sorry if this is a stupid question. But if you put rand in a loop and loop it 3 times wouldn't that provide 3 different numbers?

Yes, in that particular program. The problem the original poster had was every time he would use the program it would give the same 3 numbers as before. In order to work around this you must 'seed' the number generator with a value that will be different each time the program is run.

The next problem he had was calling srand() way too much. You're only supposed to call it once during a program, not each time you want a random number. This is because when you call time(0) it will return the time... and if you call srand more than once in a second you will give the same seed to srand(). Hope that clears that up.

And to zerotheend2000, there are no stupid questions here. Just make sure you attempt to solve problems on your own before asking for help and you'll be fine.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 15
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
634135
• Total Posts
3015754
×