|
| 1 | +/* |
| 2 | + * PCG Random Number Generation for C. |
| 3 | + * |
| 4 | + * Copyright 2014 Melissa O'Neill <oneill@pcg-random.org> |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + * |
| 18 | + * For additional information about the PCG random number generation scheme, |
| 19 | + * including its license and other licensing options, visit |
| 20 | + * |
| 21 | + * http://www.pcg-random.org |
| 22 | + */ |
| 23 | + |
| 24 | +#include <math.h> |
| 25 | +#include "rng_pcg.h" |
| 26 | + |
| 27 | +// RNG state structure is now defined in the header |
| 28 | + |
| 29 | +// global RNG state |
| 30 | +static pcg32_random_t pcg32_global = { |
| 31 | + 0x853c49e6748fea9bULL, |
| 32 | + 0xda3e39cb94b95bdbULL |
| 33 | +}; |
| 34 | + |
| 35 | +// default multi[plier] |
| 36 | +#define PCG_DEFAULT_MULTIPLIER_64 6364136223846793005ULL |
| 37 | + |
| 38 | +// helper functions |
| 39 | +static inline uint32_t pcg_rotr_32(uint32_t value, unsigned int urot) { |
| 40 | + int rot = (int)urot; |
| 41 | + return (value >> rot) | (value << ((-rot) & 31)); |
| 42 | +} |
| 43 | + |
| 44 | +static inline void pcg_setseq_64_step_r(pcg32_random_t* rng) { |
| 45 | + rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_64 + rng->inc; |
| 46 | +} |
| 47 | + |
| 48 | +static inline uint32_t pcg_output_xsh_rr_64_32(uint64_t state) { |
| 49 | + return pcg_rotr_32(((state >> 18u) ^ state) >> 27u, state >> 59u); |
| 50 | +} |
| 51 | + |
| 52 | +uint32_t pcg32_random_r(pcg32_random_t* rng) { |
| 53 | + const uint64_t oldstate = rng->state; |
| 54 | + pcg_setseq_64_step_r(rng); |
| 55 | + return pcg_output_xsh_rr_64_32(oldstate); |
| 56 | +} |
| 57 | + |
| 58 | +uint32_t pcg32_boundedrand_r(pcg32_random_t* rng, uint32_t ubound) { |
| 59 | + int32_t bound = (int32_t)ubound; |
| 60 | + uint32_t threshold = -bound % bound; |
| 61 | + for (;;) { |
| 62 | + const uint32_t r = pcg32_random_r(rng); |
| 63 | + if (r >= threshold) |
| 64 | + return r % bound; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +void pcg32_srandom_r(pcg32_random_t* rng, uint64_t initstate, uint64_t initseq) { |
| 69 | + rng->state = 0U; |
| 70 | + rng->inc = (initseq << 1u) | 1u; |
| 71 | + pcg_setseq_64_step_r(rng); |
| 72 | + rng->state += initstate; |
| 73 | + pcg_setseq_64_step_r(rng); |
| 74 | +} |
| 75 | + |
| 76 | +// public interface to RNG |
| 77 | + |
| 78 | +uint32_t pcg32_random() { |
| 79 | + return pcg32_random_r(&pcg32_global); |
| 80 | +} |
| 81 | + |
| 82 | +uint32_t pcg32_boundedrand(uint32_t bound) { |
| 83 | + return pcg32_boundedrand_r(&pcg32_global, bound); |
| 84 | +} |
| 85 | + |
| 86 | +double pcg32_frandom() { |
| 87 | + return ldexp(pcg32_random(), -32); |
| 88 | +} |
| 89 | + |
| 90 | +void pcg32_srandom(uint64_t seq) { |
| 91 | + pcg32_srandom_r(&pcg32_global, 42u, seq); |
| 92 | +} |
0 commit comments