* Fills an array with cnt random npy_uint64 between off and off + rng * inclusive. The numbers wrap if rng is sufficiently large. */
| 265 | * inclusive. The numbers wrap if rng is sufficiently large. |
| 266 | */ |
| 267 | void rk_random_uint64(npy_uint64 off, npy_uint64 rng, npy_intp cnt, |
| 268 | npy_uint64 *out, rk_state *state) { |
| 269 | npy_uint64 val, mask = rng; |
| 270 | npy_intp i; |
| 271 | |
| 272 | if (rng == 0) { |
| 273 | for (i = 0; i < cnt; i++) { |
| 274 | out[i] = off; |
| 275 | } |
| 276 | return; |
| 277 | } |
| 278 | |
| 279 | /* Smallest bit mask >= max */ |
| 280 | mask |= mask >> 1; |
| 281 | mask |= mask >> 2; |
| 282 | mask |= mask >> 4; |
| 283 | mask |= mask >> 8; |
| 284 | mask |= mask >> 16; |
| 285 | mask |= mask >> 32; |
| 286 | |
| 287 | for (i = 0; i < cnt; i++) { |
| 288 | if (rng <= 0xffffffffUL) { |
| 289 | while ((val = (rk_uint32(state) & mask)) > rng) |
| 290 | ; |
| 291 | } else { |
| 292 | while ((val = (rk_uint64(state) & mask)) > rng) |
| 293 | ; |
| 294 | } |
| 295 | out[i] = off + val; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | /* |
| 300 | * Fills an array with cnt random npy_uint32 between off and off + rng |