mz.c

Brent Chapman chapman at cory.Berkeley.EDU
Thu Nov 6 10:16:32 AEST 1986


Thhis is a neat program, but it assumes that the range of values returned
by rand() is 0 to ((2^15) - 1); this is not the case on BSD VAXen and
Suns.  On those machines (and presumeably others), the code compiles just
fine, but sooner or later dies a horrid death when oneof() returns 
something wierd.  The quick, machine independant fix is to mask off
all but the low-order 15 bits of the result of rand().  This works,
assuming that the low-order bits alone are reasonably random; this may
or may not be a valid assumption, depending on your hardware and
software.  But it's good enough for the purposes of this program.

Anyway, the definition of macro oneof() needs to be changed from:

    #define oneof(n)    (int)(((long)(n) * rand())/32768)

to:

    #define oneof(n)    (int)(((long)(n) * (rand() & 0x7fff))/32768)

This works, and is more portable than the original code (note that I
do _not_ say that it is portable, or that doing it this way is a good
idea; simply that it is _more_ portable than the original.).


Brent
--
Brent Chapman

chapman at cory.berkeley.edu	or	ucbvax!cory!chapman



More information about the Comp.sources.unix mailing list