Mail Archives: djgpp/1997/06/01/22:34:55
From: | Erik Max Francis <max AT alcyone DOT com>
|
Newsgroups: | comp.os.msdos.djgpp
|
Subject: | Re: Random numbers
|
Date: | Fri, 30 May 1997 14:04:46 -0700
|
Organization: | Alcyone Systems
|
Lines: | 23
|
Message-ID: | <338F40EE.5244E66@alcyone.com>
|
References: | <01bc6ce9$72076bc0$363e63c3 AT 8652hvt73761>
|
NNTP-Posting-Host: | newton.alcyone.com
|
Mime-Version: | 1.0
|
To: | djgpp AT delorie DOT com
|
DJ-Gateway: | from newsgroup comp.os.msdos.djgpp
|
Matthew Bennett wrote:
> When using the 'random' command in the form:
>
> printf("%d\n", random() & 10);
>
> The numbers produced are always either 0, 2, 8 or 10.
That's because you're bitwise and'ing. What you meant to do was use the
modulo operator %.
You can use the & operator for this kind of bounding when the bound is a
power of two, in which case you bitwise and with one less than that.
However, it can only be used in that special case when it is a power of
two, and 10 most certainly is not.
--
Erik Max Francis, &tSftDotIotE / email / max AT alcyone DOT com
Alcyone Systems / web / http://www.alcyone.com/max/
San Jose, California, United States / icbm / 37 20 07 N 121 53 38 W
\
"Covenants without the sword / are but words."
/ Camden
- Raw text -