I am trying to generate random numbers using c++ but they don't appear to be very random. I am of course aware that they are only pseudo random but what I have found is really not even cutting it as pseudo random. Here is my code
#include <iostream> #include <time.h> int main(){ srand (time(NULL)); std::cout << (double) rand() / (double) RAND_MAX; return 0; } The results I get from compiling and running this cpp file always starts 0.63 followed by some seemingly random numbers.
Is there something I'm missing? Am I not initializing the random number generator correctly using srand? Why arn't the numbers random between 0 and 1?
Thanks
rand()returns a number between 0 andRAND_MAX, not 0 and 1.RAND_MAXis defined as being "at least 32767". AssumingRAND_MAXis 2147483647, for example, thenrand()would have to be returning values roughly between 1352914697 and 1372242050 for your calculation to produce results between 0.630 and 0.639. That is a pretty large range of random numbers. On the other hand, you are using the old Crand()function, when you should be using C++'s own random number generators instead.RAND_MAX. Pretty depressing.#include <stdlib.h>also