[net.unix] Precision of rand

grt@twitch.UUCP ( G.R.Tomasevich) (07/20/85)

> 	It seems that I spoke too soon. Ints on our 3B20S are indeed 4 bytes
> long. rand() returns a 4 byte value BUT (this is the catch) the value is
> masked to its lower 15 bits yeilding 0 <= rand() <= 32767.
> -- 
> Edward C. Bennett

The length of the calculation has been 32 bits for a long time, even on
the PDP-11.  The static variable, randx, is a long.  See the source
routine, which is /usr/src/lib/libc/port/gen/rand.c on our machine.
-- 
	George Tomasevich, ihnp4!twitch!grt
	AT&T Bell Laboratories, Holmdel, NJ

guy@sun.uucp (Guy Harris) (07/23/85)

> > 	It seems that I spoke too soon. Ints on our 3B20S are indeed 4 bytes
> > long. rand() returns a 4 byte value BUT (this is the catch) the value is
> > masked to its lower 15 bits yeilding 0 <= rand() <= 32767.
> 
> The length of the calculation has been 32 bits for a long time, even on
> the PDP-11.  The static variable, randx, is a long.  See the source
> routine, which is /usr/src/lib/libc/port/gen/rand.c on our machine.

Yes, but the same source code reveals that the result of the calculation is
ANDed with 0x7fff before being returned.  All results that "rand" returns,
except on UNIX/32V and its descendants (i.e., 4.xBSD), and maybe on V7 ports
with 32-bit "int"s (are there any out there?), will fit in 16 bits.

	Guy Harris