Generate a random (bits)-bit integer. */
| 182 | |
| 183 | /** Generate a random (bits)-bit integer. */ |
| 184 | uint64_t randbits(int bits) noexcept |
| 185 | { |
| 186 | if (bits == 0) { |
| 187 | return 0; |
| 188 | } else if (bits > 32) { |
| 189 | return rand64() >> (64 - bits); |
| 190 | } else { |
| 191 | if (bitbuf_size < bits) FillBitBuffer(); |
| 192 | uint64_t ret = bitbuf & (~(uint64_t)0 >> (64 - bits)); |
| 193 | bitbuf >>= bits; |
| 194 | bitbuf_size -= bits; |
| 195 | return ret; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /** Generate a random integer in the range [0..range). |
| 200 | * Precondition: range > 0. |
no outgoing calls