www.digitalmars.com         C & C++   DMDScript  

digitalmars.empire - Empire D code question

reply Clay Bridges <diffengr gmail.com> writes:
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
Dec 29 2010
parent Stewart Gordon <smjg_1998 yahoo.com> writes:
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.
Jan 04 2011