are srand() and rand() 's implementations standardized?

This is a discussion on are srand() and rand() 's implementations standardized? within the C++ Programming forums, part of the General Programming Boards category; Hi, Are the implementations of srand() and rand() standardized? eg. If I use the same seed, can I expect to ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183

    are srand() and rand() 's implementations standardized?

    Hi,
    Are the implementations of srand() and rand() standardized? eg. If I use the same seed, can I expect to get the same sequence from gcc and Microsoft compiler? If not, can someone please suggest a good general-purpose (not for cryptography) pseudo-random generator?

    Thanks

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,269
    Quote Originally Posted by cyberfish View Post
    Hi,
    Are the implementations of srand() and rand() standardized? eg. If I use the same seed, can I expect to get the same sequence from gcc and Microsoft compiler? If not, can someone please suggest a good general-purpose (not for cryptography) pseudo-random generator?
    You are not guaranteed to get the same results. As for an algorithm I recommend Mersenne Twister. Search for "mt19937.c"

  3. #3
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    No.

    On my Vista machine:
    Code:
    C:\Users\...\Documents\Code>randtest
    4404, 25954, 17763, 29679, 4113, 18955, 8176, 17911, 814, 2645,
    On a FreeBSD machine I have access to:
    Code:
    $ gcc -o randtest randtest.c
    $ ./randtest
    22470959, 1859769688, 524664131, 462195135, 665282746, 1607245740, 1929840214, 1378956057, 470931575, 1469741830,
    $ cat randtest.c
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
            int x;
    
            srand(1337);
            for(x = 0; x < 10; ++x)
            {
                    printf("&#37;u, ", rand());
            }
            printf("\n");
    
            return 0;
    }
    $
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    Thank you for your help. I will certainly look into Mersenne Twister.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The TR1 libraries contain the random library which contains a framework for random number generation, including a Mersenne Twister.
    If your compiler doesn't provide a TR1 implementation yet, you can get Boost's version.

    http://boost.org/
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,256
    Incidentally, Prelude also has an implementation of the Mersenne Twister algorithm at her eternally confuzzling website. The download link is under "Libraries".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    I see... Thanks for your replies.

    I guess I will just use mt19937.c for the time being =).

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21