none
Running C++ rand and srand on different computers/OS RRS feed

  • Question

  • I need a mechanism to get pseudo-random numbers which will give me the same results whenever and wherever I use it.

    If I call srand with the same seed, and then call rand - will I always get the same random numbers (even if running on different computers, with different OS installed)?

    i.e.
      srand(20);
      int a[20];
      for( int i = 0; i < 20; ++i)
      {
           a[i] = rand();
      }

    Will the buffer a[20] always get the same 20 numbers?

    Thanks,
    Nir
    Tuesday, August 25, 2009 2:23 PM

Answers

  • No - you are not guaranteed to get the same sequence on different OS/Platforms.

    On the same operating system and platform, srand with a specific seed followed by rand will provide the same answer.

    However, platforms are free to implement rand() using multiple methods.  In fact, you're only guarantee is that it will return a value between 0 and RAND_MAX, which is a platform specific value (although, typically, 32767, it can be any positive number).

    If you need consistency, I recommend compiling your own random number generator.  You can work from one of the many online .  This would give you control over how the seeding and distribution is created.


    Reed Copsey, Jr. - http://reedcopsey.com
    • Edited by Reed Copsey, JrMVP Tuesday, August 25, 2009 3:08 PM Added suggestion
    • Marked as answer by Nir Bar Wednesday, August 26, 2009 6:17 AM
    Tuesday, August 25, 2009 3:07 PM
  • Thanks,
    I've found this site: http://www.agner.org/random/
    It provides a library/source code for a few random generators
    Nir
    • Marked as answer by Nir Bar Wednesday, August 26, 2009 6:53 AM
    Wednesday, August 26, 2009 6:53 AM

All replies

  • Yes.
    Hans Passant.
    Tuesday, August 25, 2009 3:05 PM
    Moderator
  • No - you are not guaranteed to get the same sequence on different OS/Platforms.

    On the same operating system and platform, srand with a specific seed followed by rand will provide the same answer.

    However, platforms are free to implement rand() using multiple methods.  In fact, you're only guarantee is that it will return a value between 0 and RAND_MAX, which is a platform specific value (although, typically, 32767, it can be any positive number).

    If you need consistency, I recommend compiling your own random number generator.  You can work from one of the many online .  This would give you control over how the seeding and distribution is created.


    Reed Copsey, Jr. - http://reedcopsey.com
    • Edited by Reed Copsey, JrMVP Tuesday, August 25, 2009 3:08 PM Added suggestion
    • Marked as answer by Nir Bar Wednesday, August 26, 2009 6:17 AM
    Tuesday, August 25, 2009 3:07 PM
  • That's nonsense.  "Platforms" don't implement rand(), the CRT library does.  Different builds of the CRT do not use different source code.  There's nothing inside rand() or srand() that depends on implementation details.

    Hans Passant.
    Tuesday, August 25, 2009 4:15 PM
    Moderator
  • Different C runtime libraries are free to implement rand() in any manner that returns a value between 0 and RAND_MAX.  RAND_MAX is only guaranteed to be at least 32767.  The C specification has no restriction on the implementation details of rand(), either.

    For example, in Visual Studio, the CRT defines RAND_MAX as 0x7fff (32767).  However, the GNU C Library has RAND_MAX defined as 2147483647 (the largest signed int).

    This causes rand() to have a VERY different result on Windows Vista than on Linux, with the same seed.

    Different platforms can be very, very different in their implementation.  I believe Microsoft has been nice and implemented this the same on all Microsoft platforms, but other platforms, including embedded systems, do have different implementations.
    Reed Copsey, Jr. - http://reedcopsey.com
    Tuesday, August 25, 2009 4:27 PM
  • Thanks,
    I've found this site: http://www.agner.org/random/
    It provides a library/source code for a few random generators
    Nir
    • Marked as answer by Nir Bar Wednesday, August 26, 2009 6:53 AM
    Wednesday, August 26, 2009 6:53 AM