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

fred
  • 117
  • 1
  • 1
  • 5
  • 2
    Couldn't reproduce: https://s13.postimg.org/oy2bv4izb/image.png – Khalil Khalaf Sep 08 '16 at 03:57
  • 1
    Can you add a sample of random numbers that you consider lacking? Also what platform are you running on? – Paul Rooney Sep 08 '16 at 04: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](http://en.cppreference.com/w/cpp/numeric/random) instead. – Remy Lebeau Sep 08 '16 at 04:04
  • @RemyLebeau check out Microsoft's version of `RAND_MAX`. Pretty depressing. – Mark Ransom Sep 08 '16 at 04:07
  • use `#include ` also – M.M Sep 08 '16 at 04:09
  • Are you running the program at different times and still getting the same result? – M.M Sep 08 '16 at 04:09
  • @MarkRansom: assuming such a low `RAND_MAX` were actually being used (not all C++ compilers follow Microsoft's definitions), then `rand()` would have to be producing values roughly between 20643 to 20938 to produce fred's output. That could make sense if `srand()` were being called at close intervals, since `time()` only has seconds precision. – Remy Lebeau Sep 08 '16 at 04:11
  • Microsoft's `rand` implementation goes back decades, as it doesn't change when the compiler gets updated. Internally it stores a 32 bit value, and returns 15 bits from the middle as the value of `rand`. There was a non-MS compiler on the Mac 20+ years ago that used the exact same algorithm. – 1201ProgramAlarm Sep 08 '16 at 04:17
  • I'd suggest you provide sample output from 5 consecutive runs. – Disillusioned Sep 08 '16 at 04:58

3 Answers3

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.

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
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;
}
Community
  • 1
  • 1
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.

Community
  • 1
  • 1
yoz
  • 902
  • 8
  • 20