Precision of rand()
Guy Harris
guy at sun.uucp
Tue Jul 23 20:19:47 AEST 1985
> > 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
More information about the Comp.unix
mailing list