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


Groups > comp.lang.c > #62744

Re: thread-safe random numbers...

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: thread-safe random numbers...
Date 2015-05-28 12:38 -0700
Organization None to speak of
Message-ID <87iobclch0.fsf@kst-u.example.com> (permalink)
References <mk61l9$ap4$1@speranza.aioe.org> <mk7mam$5kv$1@dont-email.me>

Show all headers | View raw


James Kuyper <jameskuyper@verizon.net> writes:
[...]
> rand() has never been the recommended way of doing serious random number
> generation - the standard imposes no meaningful restrictions on the
> quality of the algorithm it uses. Older versions of the standard
> actually gave a specific and very unsophisticated algorithm as an
> example, and many implementations actually implemented that algorithm.
> Creating your own random number generator, or borrowing someone else's,
> is the recommended approach for serious random number generation. While
> doing so, you can make it thread-safe.

The current standard still has that sample implementation (as a
non-normative example).

Some implementations have a rand() function that's even worse; unlike
the sample version in the standard, some versions return values in which
the low-order bit alternates on each call, the low-order 2 bits have a
cycle of 4, and so on.  (I think it's one of the BSDs.)

> If you're willing to restrict the portability of your code to POSIX
> platforms, and a simple 48-bit linear congruential algorithm would be
> sufficient for your needs, you can use erand48(), nrand48() or
> jrand48(), depending upon whether you want the range of the random
> numbers to be [0.0,1.0), [0,2^31) or (-2^31,2^31). The corresponding
> drand48(), lrand48() and jrand48() functions are NOT guaranteed to be
> thread-safe.

If the mediocre implementation given in the standard is good enough,
it's easy to adapt to be thread-safe.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"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 | Next in thread | Find similar | Unroll thread


Thread

thread-safe random numbers... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-05-27 20:22 -0700
  Re: thread-safe random numbers... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-05-28 11:11 -0700
  Re: thread-safe random numbers... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-05-28 11:19 -0700
    Re: thread-safe random numbers... Rosario19 <Ros@invalid.invalid> - 2015-05-29 07:28 +0200
  Re: thread-safe random numbers... James Kuyper <jameskuyper@verizon.net> - 2015-05-28 14:22 -0400
    Re: thread-safe random numbers... Keith Thompson <kst-u@mib.org> - 2015-05-28 12:38 -0700
      Re: thread-safe random numbers... James Kuyper <jameskuyper@verizon.net> - 2015-05-28 16:42 -0400
      Re: thread-safe random numbers... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-05-28 13:57 -0700
      Re: thread-safe random numbers... Nobody <nobody@nowhere.invalid> - 2015-05-29 22:42 +0100
  Re: thread-safe random numbers... Tim Rentsch <txr@alumni.caltech.edu> - 2015-07-10 11:56 -0700
    Re: thread-safe random numbers... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-10 14:03 -0700
      Re: thread-safe random numbers... Tim Rentsch <txr@alumni.caltech.edu> - 2015-07-11 13:44 -0700
  Re: thread-safe random numbers... "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-10 12:01 -0700
    Re: thread-safe random numbers... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-10 13:53 -0700
      Re: thread-safe random numbers... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-10 13:58 -0700
      Re: thread-safe random numbers... "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-10 14:04 -0700
        Re: thread-safe random numbers... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-10 14:23 -0700
          Re: thread-safe random numbers... Keith Thompson <kst-u@mib.org> - 2015-07-10 17:12 -0700
            Re: thread-safe random numbers... "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-07-10 18:58 -0700
      Re: thread-safe random numbers... Rosario19 <Ros@invalid.invalid> - 2015-07-11 11:13 +0200

csiph-web