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


Groups > comp.lang.c > #93334

Re: Learn "elusive" and "reprimand" using C!

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Learn "elusive" and "reprimand" using C!
Date 2016-09-09 12:26 -0700
Organization None to speak of
Message-ID <lnzing7u2r.fsf@kst-u.example.com> (permalink)
References <elusive-20160909182549@ram.dialup.fu-berlin.de>

Show all headers | View raw


ram@zedat.fu-berlin.de (Stefan Ram) writes:
[...]

I haven't studied the entire program, but your use of srand() stands
out.

> /** @brief returns a random number from a measurement of some
> random process.
> @details The overall quality of randomness of this function is
> not specified. In the worst case it might always return the same
> number. */
> unsigned int entropy( void )
> { return( unsigned int )( time( 0 )* time( 0 ) + time( 0 ) ); }

If time_t is a signed type (as it commonly is, this could easily
overflow, causing undefined behavior.

All three calls to time(0) will very likely return the same value, so
calling it three times probably doesn't add any more entropy.

> int main( void )
> {
>   srand( entropy() * entropy() + entropy() );
>   srand( ( unsigned int )rand() * entropy() );
>   rand(); rand(); rand();

It looks like you're arbitrarily making multiple calls to srand()
and rand() with arbitrary arguments, in the hope of getting behavior
that's somehow "more random".

If the implementation of srand() and rand() works well, then calling
srand(time(0)) once, then calling rand() once for each random number
you need, should be sufficient.  If it doesn't work well, then no
amount of tweaking is going to squeeze high-quality pseudo-random
numbers out of it.

You might consider wrapping srand() and rand() in functions that
you can update if necessary to use some system-specific PRNG.

[...]

-- 
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 — Next in thread | Find similar | Unroll thread


Thread

Re: Learn "elusive" and "reprimand" using C! Keith Thompson <kst-u@mib.org> - 2016-09-09 12:26 -0700
  Re: Learn "elusive" and "reprimand" using C! Malcolm McLean <malcolm.mclean5@btinternet.com> - 2016-09-09 13:55 -0700
    Re: Learn "elusive" and "reprimand" using C! Richard Heathfield <rjh@cpax.org.uk> - 2016-09-09 22:04 +0100
    Re: Learn "elusive" and "reprimand" using C! Keith Thompson <kst-u@mib.org> - 2016-09-09 15:26 -0700
      Re: Learn "elusive" and "reprimand" using C! Malcolm McLean <malcolm.mclean5@btinternet.com> - 2016-09-09 14:31 -0700
        Re: Learn "elusive" and "reprimand" using C! Keith Thompson <kst-u@mib.org> - 2016-09-09 17:02 -0700
        Re: Learn "elusive" and "reprimand" using C! gazelle@shell.xmission.com (Kenny McCormack) - 2016-09-09 22:55 +0000
        Re: Learn "elusive" and "reprimand" using C! jameskuyper@verizon.net - 2016-09-09 17:01 -0700
  Re: Learn "elusive" and "reprimand" using C! Paul N <gw7rib@aol.com> - 2016-09-11 08:26 -0700
    Re: Learn "elusive" and "reprimand" using C! "James R. Kuyper" <jameskuyper@verizon.net> - 2016-09-11 11:49 -0400
    Re: Learn "elusive" and "reprimand" using C! Tim Rentsch <txr@alumni.caltech.edu> - 2016-09-11 11:51 -0700
    Re: Learn "elusive" and "reprimand" using C! Keith Thompson <kst-u@mib.org> - 2016-09-11 13:58 -0700
    Re: Learn "elusive" and "reprimand" using C! David Brown <david.brown@hesbynett.no> - 2016-09-12 09:08 +0200

csiph-web