I take a programming course and we use C ++. We had a task where, at some point, we needed to encode a function that would return a random number in the [upper, lower] interval. I used the following:
lower + (int) (upper * (rand() / (RAND_MAX + 1.0)));
I did not forget to change srandusing srand((unsigned int) time(0)).
However, I get the same value every time! I asked my professor for help, and after some investigation it turned out that the first number generated rand()was not random ... Higher order bits remained unchanged, and since this implementation uses them, the end result isn "What I expected.
Is there a more elegant but simple solution than abandoning the first value or using leftovers to achieve what I want?
Thank you very much for your attention!
~ Francisco
EDIT: Thank you all for your input. I had no idea that I rand()was such a sucking RNG: P
source
share