Tweaked test/bench prng to convert 0 -> -1
Like many prngs, xorshift breaks down when the internal state is 0. The common fix is to explicitly check for this and replace with a 1 when this happens (usually when seeding, in this API we have to check every update, this is less efficient but I don't think we really care). As a slight tweak, this now checks for 0 but replaces it with -1. This makes seed=0 different from seed=1, which is nice when using seed=range(0,n) in tests/benches.
This commit is contained in:
@@ -587,6 +587,11 @@ uint32_t bench_prng(uint32_t *state) {
|
|||||||
// A simple xorshift32 generator, easily reproducible. Keep in mind
|
// A simple xorshift32 generator, easily reproducible. Keep in mind
|
||||||
// determinism is much more important than actual randomness here.
|
// determinism is much more important than actual randomness here.
|
||||||
uint32_t x = *state;
|
uint32_t x = *state;
|
||||||
|
// must be non-zero, use uintmax here so that seed=0 is different
|
||||||
|
// from seed=1 and seed=range(0,n) makes a bit more sense
|
||||||
|
if (x == 0) {
|
||||||
|
x = -1;
|
||||||
|
}
|
||||||
x ^= x << 13;
|
x ^= x << 13;
|
||||||
x ^= x >> 17;
|
x ^= x >> 17;
|
||||||
x ^= x << 5;
|
x ^= x << 5;
|
||||||
|
|||||||
@@ -603,6 +603,11 @@ uint32_t test_prng(uint32_t *state) {
|
|||||||
// A simple xorshift32 generator, easily reproducible. Keep in mind
|
// A simple xorshift32 generator, easily reproducible. Keep in mind
|
||||||
// determinism is much more important than actual randomness here.
|
// determinism is much more important than actual randomness here.
|
||||||
uint32_t x = *state;
|
uint32_t x = *state;
|
||||||
|
// must be non-zero, use uintmax here so that seed=0 is different
|
||||||
|
// from seed=1 and seed=range(0,n) makes a bit more sense
|
||||||
|
if (x == 0) {
|
||||||
|
x = -1;
|
||||||
|
}
|
||||||
x ^= x << 13;
|
x ^= x << 13;
|
||||||
x ^= x >> 17;
|
x ^= x >> 17;
|
||||||
x ^= x << 5;
|
x ^= x << 5;
|
||||||
|
|||||||
Reference in New Issue
Block a user