portable rand/srand across linux,windows,etc

J

JohnF

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.
 
E

Eric Sosman

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."
 
B

Ben Bacarisse

JohnF said:
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: <[email protected]>
 
J

JohnF

Ben Bacarisse said:
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: <[email protected]>

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.
 
K

Keith Thompson

JohnF said:
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 said:
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.
 
J

JohnF

Keith Thompson said:
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.
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.
 
K

Keith Thompson

JohnF said:
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.
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..
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top