I’m not trying to be rude, but you don’t realize what’s going on here:
uuu is a bitmask: ‘uuu’ (or (1 << bits)-1 ) in “ret = rand_holder & uuu;“ , only puts the lower ‘bit’ quantity of bits of rand_holder into ret, then it right shifts rand_holder afterward to trash them every time in the loop when it’s done. So if bits is 8, uuu is going to be 0xff No, you aren't: > > > for (;;) { > > if (rand_bits < bits) { > > rand_holder |= ((uint64_t)arc4random()) << > > rand_bits; > > > > /* > > * rand_bits will be a number between 0 and 31 > here > > * so the 0x20 bit will be empty > > * rand_bits += 32; > > */ > > rand_bits |= 32; > > } > > > > ret = rand_holder & uuu; > > rand_holder >>= bits; > > rand_bits -= bits; > > > > if (ret < upper_bound) > > return ret; > > } > > This isn't rejection sampling. This is reusing part of the rejected > sample. -- -Luke