Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | JohnF <john@please.see.sig.for.email.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: portable rand/srand across linux,windows,etc |
| Date | 2011-11-16 03:45 +0000 |
| Organization | PANIX Public Access Internet and UNIX, NYC |
| Message-ID | <j9vbk0$rnd$1@reader1.panix.com> (permalink) |
| References | <j9tngi$fm9$1@reader1.panix.com> <0.cb82fea6f5be8eb86b9f.20111115131446GMT.878vnhttmh.fsf@bsb.me.uk> <j9tqfg$cvd$1@reader1.panix.com> <lnsjlp11i3.fsf@nuthaus.mib.org> |
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 )
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
portable rand/srand across linux,windows,etc JohnF <john@please.see.sig.for.email.com> - 2011-11-15 12:55 +0000
Re: portable rand/srand across linux,windows,etc Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-11-15 08:01 -0500
Re: portable rand/srand across linux,windows,etc Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-11-15 13:14 +0000
Re: portable rand/srand across linux,windows,etc JohnF <john@please.see.sig.for.email.com> - 2011-11-15 13:46 +0000
Re: portable rand/srand across linux,windows,etc Keith Thompson <kst-u@mib.org> - 2011-11-15 14:09 -0800
Re: portable rand/srand across linux,windows,etc JohnF <john@please.see.sig.for.email.com> - 2011-11-16 03:45 +0000
Re: portable rand/srand across linux,windows,etc Keith Thompson <kst-u@mib.org> - 2011-11-15 20:11 -0800
csiph-web