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

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

    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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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,229
    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,895
    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
    28,413
    Incidentally, Prelude also has an implementation of the Mersenne Twister algorithm at her eternally confuzzling website. The download link is under "Libraries".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,229
    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