1

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

9
  • 2
    Couldn't reproduce: s13.postimg.org/oy2bv4izb/image.png Commented Sep 8, 2016 at 3:57
  • 1
    Can you add a sample of random numbers that you consider lacking? Also what platform are you running on? Commented Sep 8, 2016 at 4:04
  • rand() returns a number between 0 and RAND_MAX, not 0 and 1. RAND_MAX is defined as being "at least 32767". Assuming RAND_MAX is 2147483647, for example, then rand() 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 C rand() function, when you should be using C++'s own random number generators instead. Commented Sep 8, 2016 at 4:04
  • @RemyLebeau check out Microsoft's version of RAND_MAX. Pretty depressing. Commented Sep 8, 2016 at 4:07
  • use #include <stdlib.h> also Commented Sep 8, 2016 at 4:09

3 Answers 3

4

It looks like the implementation of rand on your compiler is very poor, where the initial random value is highly dependent on the seed value. You can generate and throw away several random values before you start using the returned values.

Or, better still, don't call rand. Use the facilities provided by the <random> header.

Sign up to request clarification or add additional context in comments.

1 Comment

I know from experience that Microsoft's rand has this property.
2

You can try this:

struct timeval; gettimeofday(&time, NULL); srand(hash3(time.tv_sec, time.tv_usec, getpid())); 

hash3:

unsigned int hash3(unsigned int h1, unsigned int h2, unsigned int h3) { return ((h1 * 2654435789U) + h2) * 2654435789U) + h3; } 

Comments

0

The fundamental problem is that rand() is described by the C manual as a "bad random number generator". You should use the <random> library instead. See

What difference between rand() and random() functions?

Additionally, it's unclear if you are intentionally only comparing the first value generated after seeding the generator with srand(), but generally, pseudorandom number generators are used by seeding them once and then generating a series of numbers.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.