portable rand/srand across linux,windows,etc

Discussion in 'C Programming' started by JohnF, Nov 15, 2011.

  1. JohnF

    JohnF Guest

    Sorry if the wrong ng (please suggest another).
    I'm looking for robustly portable stdlib entry points
    for rand()/srand() (e.g., or random()/srandom()),
    particularly across linux gcc and windows
    mingw and djgpp (and ms compilers).
    That is, I want to distribute one source, without
    a lot of pesky #ifdef's, to compile on all platforms.
    Would also be nice if portable across everything
    else from free/netbsd to vms/dec C, too, but linux
    and windows are the biggies. Google shows various
    occasional complaints about rand stuff not compiling and/or
    not working on windows. I don't need anything particularly
    good -- just naked eye random, so to speak. And if it's
    easier to just cut-and-paste source from Numerical Recipes,
    I'm okay with that, too.
    --
    John Forkosh ( mailto: where j=john and f=forkosh )
     
    JohnF, Nov 15, 2011
    #1
    1. Advertising

  2. JohnF

    Eric Sosman Guest

    On 11/15/2011 7:55 AM, JohnF wrote:
    > Sorry if the wrong ng (please suggest another).
    > I'm looking for robustly portable stdlib entry points
    > for rand()/srand() (e.g., or random()/srandom()),
    > particularly across linux gcc and windows
    > mingw and djgpp (and ms compilers).
    > That is, I want to distribute one source, without
    > a lot of pesky #ifdef's, to compile on all platforms.
    > Would also be nice if portable across everything
    > else from free/netbsd to vms/dec C, too, but linux
    > and windows are the biggies. Google shows various
    > occasional complaints about rand stuff not compiling and/or
    > not working on windows. I don't need anything particularly
    > good -- just naked eye random, so to speak. And if it's
    > easier to just cut-and-paste source from Numerical Recipes,
    > I'm okay with that, too.


    Every (hosted) C implementation supplies rand() and srand(),
    both declared in <stdlib.h>. The quality of the generated
    numbers may vary from one implementation to the next, but should
    suffice for "naked eye random."

    --
    Eric Sosman
    d
     
    Eric Sosman, Nov 15, 2011
    #2
    1. Advertising

  3. JohnF <> writes:

    > Sorry if the wrong ng (please suggest another).
    > I'm looking for robustly portable stdlib entry points
    > for rand()/srand() (e.g., or random()/srandom()),
    > particularly across linux gcc and windows
    > mingw and djgpp (and ms compilers).
    > That is, I want to distribute one source, without
    > a lot of pesky #ifdef's, to compile on all platforms.
    > Would also be nice if portable across everything
    > else from free/netbsd to vms/dec C, too, but linux
    > and windows are the biggies. Google shows various
    > occasional complaints about rand stuff not compiling and/or
    > not working on windows. I don't need anything particularly
    > good -- just naked eye random, so to speak. And if it's
    > easier to just cut-and-paste source from Numerical Recipes,
    > I'm okay with that, too.


    rand and srand are documented in the C standard (and not only the most
    recent -- they date back to C90) so they are amongst the most portable
    functions you could call. I can't imagine that there's a Windows C
    implementation without these (but I've been surprised before by
    Windows).

    If you end up cut-and-pasting code, search the archives of this group
    for KISS. George Marsaglia (sadly no longer with us) has posted several
    highly efficient PRNGs with very long periods here. He was not a C
    expert, but there have been followups that have correctly portability
    issues where they've been spotted. Here's a reference to one:
    Message-ID: <4dbd6e9c$0$12957$>

    --
    Ben.
     
    Ben Bacarisse, Nov 15, 2011
    #3
  4. JohnF

    JohnF Guest

    Ben Bacarisse <> wrote:
    > JohnF <> writes:
    >> Sorry if the wrong ng (please suggest another).
    >> I'm looking for robustly portable stdlib entry points
    >> for rand()/srand() (e.g., or random()/srandom()),
    >> particularly across linux gcc and windows
    >> mingw and djgpp (and ms compilers).
    >> That is, I want to distribute one source, without
    >> a lot of pesky #ifdef's, to compile on all platforms.
    >> Would also be nice if portable across everything
    >> else from free/netbsd to vms/dec C, too, but linux
    >> and windows are the biggies. Google shows various
    >> occasional complaints about rand stuff not compiling and/or
    >> not working on windows. I don't need anything particularly
    >> good -- just naked eye random, so to speak. And if it's
    >> easier to just cut-and-paste source from Numerical Recipes,
    >> I'm okay with that, too.

    >
    > rand and srand are documented in the C standard (and not only the most
    > recent -- they date back to C90) so they are amongst the most portable
    > functions you could call. I can't imagine that there's a Windows C
    > implementation without these (but I've been surprised before by
    > Windows).
    >
    > If you end up cut-and-pasting code, search the archives of this group
    > for KISS. George Marsaglia (sadly no longer with us) has posted several
    > highly efficient PRNGs with very long periods here. He was not a C
    > expert, but there have been followups that have correctly portability
    > issues where they've been spotted. Here's a reference to one:
    > Message-ID: <4dbd6e9c$0$12957$>


    Thanks, Eric and Ben. Yeah, I'd already distributed source using
    srand()/rand(), and then came across a few google complaints about
    mingw not having them. So I thought I'd double-check while the fan
    was still clean (no problem emails yet). To cut-and-paste, by the way,
    even my K&R 2nd ed (also, sadly, just recently no longer with us)
    has a quick-and-dirty 10-line stdlib version on pg.46 that I probably
    would've used. So it seems pretty standard even back to then.
    --
    John Forkosh ( mailto: where j=john and f=forkosh )
     
    JohnF, Nov 15, 2011
    #4
  5. JohnF <> writes:
    [...]
    > Thanks, Eric and Ben. Yeah, I'd already distributed source using
    > srand()/rand(), and then came across a few google complaints about
    > mingw not having them. So I thought I'd double-check while the fan
    > was still clean (no problem emails yet).


    I'm skeptical of such complaints. For example, the following program
    compiles and executes with no errors or warnings under MinGW:

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    int main(void) {
    srand(time(NULL));
    printf("%d\n", rand());
    return 0;
    }

    Admittedly I'm using a quite recent version of MinGW, but I woudn't
    expect older versions to have any problems with this either.

    My best guess is that somebody tried to use srand() and/or rand()
    without the required "#include <stdlib.h>".

    > To cut-and-paste, by the way,
    > even my K&R 2nd ed (also, sadly, just recently no longer with us)
    > has a quick-and-dirty 10-line stdlib version on pg.46 that I probably
    > would've used. So it seems pretty standard even back to then.


    The 2nd edition of K&R is based on the 1989 ANSI C standard (which is
    also, in effect, the 1990 ISO C standard). You're not likely to find a
    C implementation (at least a hosted implementation) that doesn't support
    srand() and rand() in the manner required by the standard.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Nov 15, 2011
    #5
  6. JohnF

    JohnF Guest

    Keith Thompson <> wrote:
    > JohnF <> writes:
    > [...]
    >> Thanks, Eric and Ben. Yeah, I'd already distributed source using
    >> srand()/rand(), and then came across a few google complaints about
    >> mingw not having them. So I thought I'd double-check while the fan
    >> was still clean (no problem emails yet).

    >
    > I'm skeptical of such complaints. For example, the following program
    > compiles and executes with no errors or warnings under MinGW:
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <time.h>
    > int main(void) {
    > srand(time(NULL));
    > printf("%d\n", rand());
    > return 0;
    > }
    > Admittedly I'm using a quite recent version of MinGW, but I woudn't
    > expect older versions to have any problems with this either.
    > My best guess is that somebody tried to use srand() and/or rand()
    > without the required "#include <stdlib.h>".


    Thanks, Keith. Yeah, mingw is what I use on windows, too, and
    probably should have tried it before posting, but I (dual) boot win
    so infrequently (still using w2k) that the pain-in-the-neck reboot
    (haven't installed a hypervisor) dissuaded me from bothering.
    And now that you mention it, the time.h stuff, struct tm and
    localtime(), etc, looks-and-feels (to me) much more unixy than
    rand(), but they've worked fine in my (intended-to-be-)portable
    sources across all platforms for years.

    >> To cut-and-paste, by the way,
    >> even my K&R 2nd ed (also, sadly, just recently no longer with us)
    >> has a quick-and-dirty 10-line stdlib version on pg.46 that I probably
    >> would've used. So it seems pretty standard even back to then.

    >
    > The 2nd edition of K&R is based on the 1989 ANSI C standard (which is
    > also, in effect, the 1990 ISO C standard). You're not likely to find a
    > C implementation (at least a hosted implementation) that doesn't support
    > srand() and rand() in the manner required by the standard.


    You made me look -- my 1st ed K&R, (c) 1978, doesn't mention rand(),
    or any random number generator, at all, so I guess it crept in
    sometime between. I don't follow the history much, even though
    I (sadly) lived and programmed through a lot of it.
    --
    John Forkosh ( mailto: where j=john and f=forkosh )
     
    JohnF, Nov 16, 2011
    #6
  7. JohnF <> writes:
    [...]
    > Thanks, Keith. Yeah, mingw is what I use on windows, too, and
    > probably should have tried it before posting, but I (dual) boot win
    > so infrequently (still using w2k) that the pain-in-the-neck reboot
    > (haven't installed a hypervisor) dissuaded me from bothering.
    > And now that you mention it, the time.h stuff, struct tm and
    > localtime(), etc, looks-and-feels (to me) much more unixy than
    > rand(), but they've worked fine in my (intended-to-be-)portable
    > sources across all platforms for years.


    A lot of the C standard library does look "Unixy", for historical
    reasons, but it's part of the C language definition, and all hosted
    implementations must support it.

    You can see the latest draft of the C99 standard at
    http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf
    It will help you find out which functions are part of C itself,
    and which are system-specific.

    >>> To cut-and-paste, by the way,
    >>> even my K&R 2nd ed (also, sadly, just recently no longer with us)
    >>> has a quick-and-dirty 10-line stdlib version on pg.46 that I probably
    >>> would've used. So it seems pretty standard even back to then.

    >>
    >> The 2nd edition of K&R is based on the 1989 ANSI C standard (which is
    >> also, in effect, the 1990 ISO C standard). You're not likely to find a
    >> C implementation (at least a hosted implementation) that doesn't support
    >> srand() and rand() in the manner required by the standard.

    >
    > You made me look -- my 1st ed K&R, (c) 1978, doesn't mention rand(),
    > or any random number generator, at all, so I guess it crept in
    > sometime between. I don't follow the history much, even though
    > I (sadly) lived and programmed through a lot of it.


    Right, the 1st edition describes a pre-standard version of the language.

    A standard was issued by ANSI in 1989 and adopted by ISO in 1990; that's
    the version of the language described by the 2nd edition of K&R.

    A revised standard was issued by ISO in 1999. (There's no 3rd edition
    of K&R describing C99.) The C99 standard is very nearly backward
    compatible with the C90 standard, and not all compilers fully implement
    C99, so if you follow the language described in K&R2 (except perhaps for
    the Unix-specific section, which is clearly marked), you should be as
    far as portability is concerned..

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Nov 16, 2011
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. August1
    Replies:
    4
    Views:
    7,727
    August1
    Dec 8, 2003
  2. August1
    Replies:
    0
    Views:
    7,508
    August1
    May 16, 2004
  3. August1
    Replies:
    0
    Views:
    687
    August1
    May 16, 2004
  4. Bill Cunningham

    rand and srand

    Bill Cunningham, Mar 9, 2008, in forum: C Programming
    Replies:
    57
    Views:
    1,403
    Keith Thompson
    May 8, 2008
  5. Arijit Das

    srand versus srandom - srand with random() safe?

    Arijit Das, Oct 17, 2011, in forum: C Programming
    Replies:
    12
    Views:
    3,027
    Nick Keighley
    Oct 18, 2011
Loading...

Share This Page