Article View: net.bugs
Article #411Re: Bug in rand() and srand()
From: Tim Smith
Date: Tue, 05 Mar 1985 01:31
Date: Tue, 05 Mar 1985 01:31
30 lines
1829 bytes
1829 bytes
In article <320@cubsvax.UUCP> peters@cubsvax.UUCP (Peter S. Shenkin) writes:
>In attempting to use these functions under 4.1bsd on a VAX/780, I found that
>rand() returns strictly alternating even and odd numbers, regardless of
>seed!!! So I went and tried the same thing on a 4.2bsd system, and found
>the same behavior.
>
If BSD rand() is at all like System V rand(), this is not a bug. Rand()
is a multiplicitive congruential (sp?) random number generator, which means
it works like this:
R = A * R + C mod M
i+1 i
So, depending on the parity of A and C, you have one of the following cases:
Ri+1 is always odd
Ri+1 is always even
Ri+1 is always same parity as Ri
or Ri+1 is always opposite parity of Ri
In this sort of number generator, the high bits are "more random" than the
low bits. Thus, for example, to get a stream or random bits, one would
look at the top bit ( assuming M is a power of two... ), not the bottom bit.
Look at Knuth, Vol. II for almost all that is known on this topic.
--
Duty Now for the Future
Tim Smith
ihnp4!wlbr!callan!tim or ihnp4!cithep!tim
Technical Details
Message-ID:
<307@callan.UUCP>Path:
arc1-ca.pugleaf.net!utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!godot!harvard!seismo!umcp-cs!gymble!lll-crg!dual!callan!timReferences:
<320@cubsvax.UUCP>Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10 beta 3/9/83; site callan.UUCP Path: arc1-ca.pugleaf.net!utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!godot!harvard!seismo!umcp-cs!gymble!lll-crg!dual!callan!tim From: tim@callan.UUCP (Tim Smith) Newsgroups: net.bugs,net.unix,net.lang.c,net.math Subject: Re: Bug in rand() and srand() Message-ID: <307@callan.UUCP> Date: Mon, 4-Mar-85 20:31:30 EST Article-I.D.: callan.307 Posted: Mon Mar 4 20:31:30 1985 Date-Received: Sat, 9-Mar-85 07:26:12 EST References: <320@cubsvax.UUCP> Reply-To: tim@callan.UUCP (Tim Smith) Organization: Callan Data Systems, Westlake Village, CA Lines: 30