Hi!
For random numbers I use "randomize();" and "random(number);". But if I have this in a loop, then the random number is always the same. Why is that so? Please help.
Hi!
For random numbers I use "randomize();" and "random(number);". But if I have this in a loop, then the random number is always the same. Why is that so? Please help.
Current projects:
1) User Interface Development Kit (C++)
2) HTML SDK (C++)
3) Classes (C++)
4) INI Editor (Delphi)
Something in my .sig should help... can you guess what it is?
1) Read the FAQ.
2) Put randomize outside of your loop.
3) Read the FAQ.
4) Better yet, use 'srand()' outside your loop instead.
5) Read the FAQ.
6) Call your number generation inside the loop. 'rand()'
7) Read the FAQ.
Quzah.
Hope is the first step on the road to disappointment.
Hey GaPe,
First you need to seed the randomizer. You can use randomize() or do it like this:
srand((unsigned)time(NULL));
Remember to only seed the randomiser ONCE - i.e. OUTSIDE of you main program loop.
Whenever you want to get a random number just use:
rand() % range;
where range is the maximum number.
p.s. I think you will need to include <time.h> to seed the randomizer.
x=rand() % range;
wouldn't that give you an answer like
0 through range -1 ?
Yup. So just add one to it to get 1 to range.x=rand() % range;
wouldn't that give you an answer like
0 through range -1 ?
x = 1 + rand() % range;
Quzah.
Hope is the first step on the road to disappointment.
Thank you guys. It is working now.
Current projects:
1) User Interface Development Kit (C++)
2) HTML SDK (C++)
3) Classes (C++)
4) INI Editor (Delphi)