Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #13459

Re: portable rand/srand across linux,windows,etc

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: portable rand/srand across linux,windows,etc
Date 2011-11-15 20:11 -0800
Organization None to speak of
Message-ID <lnty64zoy3.fsf@nuthaus.mib.org> (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> <j9vbk0$rnd$1@reader1.panix.com>

Show all headers | View raw


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/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) 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"

Back to comp.lang.c | Previous | Next — Previous in thread | Find similar | Unroll thread


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