Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   portable rand/srand across linux,windows,etc (http://www.velocityreviews.com/forums/t805940-portable-rand-srand-across-linux-windows-etc.html)

JohnF 11-15-2011 12:55 PM

portable rand/srand across linux,windows,etc
 
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: j@f.com where j=john and f=forkosh )

Eric Sosman 11-15-2011 01:01 PM

Re: portable rand/srand across linux,windows,etc
 
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
esosman@ieee-dot-org.invalid

Ben Bacarisse 11-15-2011 01:14 PM

Re: portable rand/srand across linux,windows,etc
 
JohnF <john@please.see.sig.for.email.com> 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$892e0abb@auth.newsreader.octanew s.com>

--
Ben.

JohnF 11-15-2011 01:46 PM

Re: portable rand/srand across linux,windows,etc
 
Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
> JohnF <john@please.see.sig.for.email.com> 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$892e0abb@auth.newsreader.octanew s.com>


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: j@f.com where j=john and f=forkosh )

Keith Thompson 11-15-2011 10:09 PM

Re: portable rand/srand across linux,windows,etc
 
JohnF <john@please.see.sig.for.email.com> 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) kst-u@mib.org <http://www.ghoti.net/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

JohnF 11-16-2011 03:45 AM

Re: portable rand/srand across linux,windows,etc
 
Keith Thompson <kst-u@mib.org> wrote:
> JohnF <john@please.see.sig.for.email.com> 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: j@f.com where j=john and f=forkosh )

Keith Thompson 11-16-2011 04:11 AM

Re: portable rand/srand across linux,windows,etc
 
JohnF <john@please.see.sig.for.email.com> 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/wg...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) kst-u@mib.org <http://www.ghoti.net/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


All times are GMT. The time now is 11:45 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57