Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > portable rand/srand across linux,windows,etc

Reply
Thread Tools

portable rand/srand across linux,windows,etc

 
 
JohnF
Guest
Posts: n/a
 
      11-15-2011
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: http://www.velocityreviews.com/forums/(E-Mail Removed) where j=john and f=forkosh )
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      11-15-2011
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
(E-Mail Removed)d
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      11-15-2011
JohnF <(E-Mail Removed)> 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$(E-Mail Removed) s.com>

--
Ben.
 
Reply With Quote
 
JohnF
Guest
Posts: n/a
 
      11-15-2011
Ben Bacarisse <(E-Mail Removed)> wrote:
> JohnF <(E-Mail Removed)> 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$(E-Mail Removed) 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: (E-Mail Removed) where j=john and f=forkosh )
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      11-15-2011
JohnF <(E-Mail Removed)> 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) (E-Mail Removed) <http://www.ghoti.net/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
JohnF
Guest
Posts: n/a
 
      11-16-2011
Keith Thompson <(E-Mail Removed)> wrote:
> JohnF <(E-Mail Removed)> 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: (E-Mail Removed) where j=john and f=forkosh )
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      11-16-2011
JohnF <(E-Mail Removed)> 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) (E-Mail Removed) <http://www.ghoti.net/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Portable Python - free portable development environment ! perica.zivkovic@gmail.com Python 7 01-13-2007 11:19 AM
portable (VHDL) vs. non-portable (altera LPM) approaches to signed computations Eli Bendersky VHDL 1 03-01-2006 02:43 PM
Vantec Portable USB2 Docking Station Video Review #599: Silverstrand Front Page News 0 09-15-2005 02:59 AM
Logitech MM22 Portable iPod Speakers @ BonaFideReviews Silverstrand Front Page News 0 08-05-2005 03:07 AM
Ultra Mini Portable Disk Enclosure Review @ PC Modding Malay Silverstrand Front Page News 0 07-01-2005 01:33 AM



Advertisments