Hello, :hello:
I'm searching a way to generate a random number. After a search about this, I've tried this, including time.h :
I put it in ClbkSetClassCaps, but I'm not sure it matters a lot.
The problem is that, while I should get a number from 0 to 65535, I get a very narrow array of numbers, like 7200-7500.
From what I understood, this isn't a big surprise since this method relies on the time that it took to reach the line of code. I'm not surprised that the variations are limited, because the clock of my CPU is steady enough.
So it really isn't a random number at all, it will just give different results on different systems. Not what I want to simulate things like system failures.
So I'm taking any advice to generate "reasonably random" numbers. I know that computer can't generate "genuinely random" numbers, but there is a way to do a lot better than what I get (as there are good roulette or other casino games simulations)...
Thanks in advance, :tiphat:
Edit : and using what follows, I always get "38" ! What a randomness !
srand((unsigned)time(0));
int lowest=1, highest=100;
int range=(highest-lowest)+1;
int r = lowest+int(range*rand()/(RAND_MAX + 1.0));
I'm searching a way to generate a random number. After a search about this, I've tried this, including time.h :
Code:
srand ( (unsigned int) time(NULL) );
r = rand();
I put it in ClbkSetClassCaps, but I'm not sure it matters a lot.
The problem is that, while I should get a number from 0 to 65535, I get a very narrow array of numbers, like 7200-7500.
From what I understood, this isn't a big surprise since this method relies on the time that it took to reach the line of code. I'm not surprised that the variations are limited, because the clock of my CPU is steady enough.
So it really isn't a random number at all, it will just give different results on different systems. Not what I want to simulate things like system failures.
So I'm taking any advice to generate "reasonably random" numbers. I know that computer can't generate "genuinely random" numbers, but there is a way to do a lot better than what I get (as there are good roulette or other casino games simulations)...
Thanks in advance, :tiphat:
Edit : and using what follows, I always get "38" ! What a randomness !
srand((unsigned)time(0));
int lowest=1, highest=100;
int range=(highest-lowest)+1;
int r = lowest+int(range*rand()/(RAND_MAX + 1.0));
Last edited: