December 29, 2010 Empire D code question | ||||
---|---|---|---|---|
| ||||
I see code like this: if (ranq() & 8) ... where ranq() is defined as uint ranq() { return std.random.rand(); } Would that be a coin toss, and similar to , e.g. (ranq() & 1)? Thanks Clay |
January 04, 2011 Re: Empire D code question | ||||
---|---|---|---|---|
| ||||
Posted in reply to Clay Bridges | On 29/12/2010 22:41, Clay Bridges wrote: > I see code like this: > > if (ranq()& 8) ... > > where ranq() is defined as > > uint ranq() { return std.random.rand(); } Good question - why is this wrapper there? > Would that be a coin toss, and similar to , e.g. (ranq()& 1)? Yes. Since 8 has only one set bit, there are only two possible values - 0 and 8. I don't know why 8 was chosen. I just searched the code and found instances of: ranq() & 1 (1/2 chance) ranq() & 4 (1/2 chance) ranq() & 8 (1/2 chance) ranq() & 7 (1/8 chance) ranq() & 15 (1/16 chance) Stewart. |
Copyright © 1999-2021 by the D Language Foundation