Bug in random number generator - (nf)

utzoo!decvax!ucbvax!cbosgd!mark utzoo!decvax!ucbvax!cbosgd!mark
Fri Mar 25 10:44:54 AEST 1983


It is curious that V7's rand produces 15 bit random integers,
but 32V (and hence 4.1BSD) produce 31 bit numbers.  This makes
it interesting trying to write portable programs.  USG UNIX
rand's produce 15 bit numbers, so the proper direction does
indeed seem to be toward 15 bits.

Let me add my two cents worth to the comment about the low
order bits.  It's certainly convenient to use the % operator
to get a random integer in a given range.  But it's simply
not very random.  Here is a macro that will do the job
much more randomly on 4.1BSD (and, presumably, on other UNIX
systems)

#define randint(n) ( ((long) rand() & 0177777) * n / 0177777)
n is the range you want, e.g. randint(10) will produce a random
integer in the range 0 through 9.



More information about the Comp.bugs.4bsd.ucb-fixes mailing list