random routines
This commit is contained in:
31
test_scripts/pseudorandom_gens/notes.txt
Normal file
31
test_scripts/pseudorandom_gens/notes.txt
Normal file
@ -0,0 +1,31 @@
|
||||
|
||||
|
||||
Mark Dickinson
|
||||
Over a year ago
|
||||
Any thoughts on the PCG family? (E.g., the generator called "PCG RXS M XS 32 (LCG)" in the PCG paper.)
|
||||
bryc
|
||||
Over a year ago
|
||||
@MarkDickinson From what I've seen PCG relies on 64-bit math, and there are no fully 32-bit variants that I can find. I can't find any code specific to the generator you mentioned either. I'm sure PCG is a good generator, but I feel like its a poor choice for JavaScript or embedded systems. xoshiro128** for example is ideal in that regard, since it uses 32-bit integers only.
|
||||
|
||||
https://www.pcg-random.org/posts/some-prng-implementations.html
|
||||
|
||||
JSF: Bob Jenkins's Small/Fast Chaotic PRNG
|
||||
|
||||
The gist jsf.hpp contains an implementation of Bob Jenkins's “Small/Fast PRNG”, which is based on a random invertible mapping—what some call a “chaotic PRNG”. I discussed this generator in a previous post, but the short version is that it passes stringent statistical tests, seems to be quite annoying to predict, and works well. It's also very fast.
|
||||
|
||||
My C++ implementation provides the standard jsf64 and jsf32 variants, as well as a number of variations Jenkins suggests that use different constants that should also work well. It also includes some tiny versions, jsf16 and jsf8, which are mostly designed for experimental use (these smaller variants should not be expected to pass extensive statistical tests).
|
||||
|
||||
GJrand: David Blackman's Chaotic PRNG
|
||||
|
||||
The gist gjrand.hpp contains an implementation of David Blackman's gjrand PRNG, which is based on a random invertible mapping that includes a counter to guarantee no small cycles. I will discuss this generator in a future post, but the short version is that it passes stringent statistical tests, and works well. It's also fast, although not quite as fast as JSF, but on the other hand appears to have slightly stronger bit-mixing properties than JSF.
|
||||
|
||||
My C++ implementation provides the standard gjrand64 variant, and at my request Blackman also made a gjrand32 variant. It also includes some tiny versions, gjrand16 and gjrand8, which are mostly designed for experimental use (these smaller variants should not be expected to pass extensive statistical tests).
|
||||
|
||||
SplitMix: 32-Bit and 64-Bit Output from 128-Bit State
|
||||
|
||||
The gist splitmix.hpp provides a C++ implementation of SplitMix, as described by Guy L. Steele, Jr., Doug Lea and Christine H. Flood in the paper Fast Splittable Pseudorandom Number Generators and implemented in Java 8 as SplittableRandom (cannonical source code).
|
||||
|
||||
This C++ implementation avoids the bugs present in implementations directly derived from the (erroneous) code in the SplitMix paper. Without these bugs it has all properties, good and bad, that are inherent in SplitMix's design (discussed at length in previous posts). In particular, the 64-bit–output variant, splitmix64, may not be suitable for general-purpose use because it has the property that each number is only output once (similar to _once_insecure PCG variants), which can be detected as a deviation from random behavior by statistical tests. The 32-bit–output variant, splitmix32, does not have this issue.
|
||||
|
||||
Unlike the other implementations described in this post, this implementation includes both jump-ahead and distance operations (most implementations of SplitMix do not offer these features, although they are quite easy to provide). Also, in contrast to other implementations, the two variants are represented as separate classes (although it is possible to cast between them; splitmix32 is a subclass of splitmix64).
|
||||
|
||||
236
test_scripts/pseudorandom_gens/splitmix.hpp
Normal file
236
test_scripts/pseudorandom_gens/splitmix.hpp
Normal file
@ -0,0 +1,236 @@
|
||||
//ref: https://gist.github.com/imneme/6179748664e88ef3c34860f44309fc71
|
||||
//ref: Guy L. Steele, Jr., Doug Lea and Christine H. Flood in the paper Fast Splittable Pseudorandom Number Generators and implemented in Java 8 as SplittableRandom (cannonical source code).
|
||||
|
||||
#ifndef SPLITMIX_HPP_INCLUDED
|
||||
#define SPLITMIX_HPP_INCLUDED 1
|
||||
|
||||
/*
|
||||
* A C++ implementation of SplitMix
|
||||
* Original design by Guy L. Steele, Jr., Doug Lea and Christine H. Flood
|
||||
* Described in _Fast splittable pseudorandom number generators_
|
||||
* http://dx.doi.org/10.1145/2714064.2660195 and implemented in
|
||||
* Java 8 as SplittableRandom
|
||||
* Based on code from the original paper, with revisions based on changes
|
||||
* made to the the Java 8 source, at
|
||||
* http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/tip/
|
||||
* src/share/classes/java/util/SplittableRandom.java
|
||||
* and other publicly available implementations.
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2018, 2025 Melissa E. O'Neill
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace splitmix_detail {
|
||||
|
||||
template <typename IntRep>
|
||||
IntRep fast_exp(IntRep x, IntRep power)
|
||||
{
|
||||
IntRep result = IntRep(1);
|
||||
IntRep multiplier = x;
|
||||
while (power != IntRep(0)) {
|
||||
IntRep thismult = power & IntRep(1) ? multiplier : IntRep(1);
|
||||
result *= thismult;
|
||||
power >>= 1;
|
||||
multiplier *= multiplier;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename IntRep>
|
||||
inline IntRep modular_inverse(IntRep x)
|
||||
{
|
||||
return fast_exp(x, IntRep(-1));
|
||||
}
|
||||
|
||||
|
||||
#if __cpp_lib_bitops > 201907L
|
||||
|
||||
// If possible, use trivial wrapper for C++20's std::popcount
|
||||
|
||||
#include <bit>
|
||||
|
||||
template <typename IntRep>
|
||||
constexpr inline IntRep pop_count(IntRep x) {
|
||||
return std::popcount(x);
|
||||
}
|
||||
|
||||
#elif !defined(__GNUC__)
|
||||
|
||||
// Fallback code based on code at https://en.wikipedia.org/wiki/Hamming_weight
|
||||
// use a popcount intrinsic if you can since most modern CPUs have one.
|
||||
|
||||
template <typename IntRep>
|
||||
inline unsigned int pop_count(IntRep x) {
|
||||
unsigned int count = 0;
|
||||
while (x) {
|
||||
++count;
|
||||
x &= x - IntRep(1);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
template <typename IntRep>
|
||||
inline IntRep pop_count(IntRep x) {
|
||||
static_assert(sizeof(IntRep) <= sizeof(unsigned long long),
|
||||
"IntRep must be no larger than unsigned long long");
|
||||
if (sizeof(IntRep) <= sizeof(unsigned int))
|
||||
return __builtin_popcount(x);
|
||||
else if (sizeof(IntRep) <= sizeof(unsigned long))
|
||||
return __builtin_popcountl(x);
|
||||
else
|
||||
return __builtin_popcountll(x);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
template <uint64_t m1, uint64_t m2,
|
||||
unsigned int p, unsigned int q, unsigned int r,
|
||||
uint64_t m3, uint64_t m4,
|
||||
unsigned int s, unsigned int t, unsigned int u>
|
||||
class splitmix64_base {
|
||||
public:
|
||||
using result_type = uint64_t;
|
||||
static constexpr result_type min() { return 0; };
|
||||
static constexpr result_type max() { return ~result_type(0); };
|
||||
|
||||
protected:
|
||||
|
||||
uint64_t seed_;
|
||||
uint64_t gamma_;
|
||||
|
||||
static uint64_t mix_gamma(uint64_t x) {
|
||||
x ^= x >> p;
|
||||
x *= m1;
|
||||
x ^= x >> q;
|
||||
x *= m2;
|
||||
x ^= x >> r;
|
||||
x |= 1ul;
|
||||
int n = pop_count(x ^ (x >> 1));
|
||||
return (n < 24) ? x ^ 0xaaaaaaaaaaaaaaaa : x;
|
||||
}
|
||||
|
||||
static uint64_t mix64(uint64_t x) {
|
||||
x ^= x >> s;
|
||||
x *= m3;
|
||||
x ^= x >> t;
|
||||
x *= m4;
|
||||
x ^= x >> u;
|
||||
return x;
|
||||
}
|
||||
|
||||
void advance() {
|
||||
seed_ += gamma_;
|
||||
}
|
||||
|
||||
uint64_t next_seed() {
|
||||
uint64_t result = seed_;
|
||||
advance();
|
||||
return result;
|
||||
}
|
||||
|
||||
public:
|
||||
splitmix64_base(uint64_t seed = 0xbad0ff1ced15ea5e,
|
||||
uint64_t gamma = 0x9e3779b97f4a7c15)
|
||||
: seed_(seed), gamma_(gamma | 1)
|
||||
{
|
||||
// Nothing (else) to do.
|
||||
}
|
||||
|
||||
uint64_t operator()() {
|
||||
return mix64(next_seed());
|
||||
}
|
||||
|
||||
void advance(uint64_t delta) {
|
||||
seed_ += delta * gamma_;
|
||||
}
|
||||
|
||||
void backstep(uint64_t delta) {
|
||||
advance(-delta);
|
||||
}
|
||||
|
||||
bool wrapped() {
|
||||
return seed_ == 0;
|
||||
}
|
||||
|
||||
uint64_t seed() const {
|
||||
return seed_;
|
||||
}
|
||||
uint64_t gamma() const {
|
||||
return gamma_;
|
||||
}
|
||||
|
||||
uint64_t operator-(const splitmix64_base& other) {
|
||||
return (seed_ - other.seed_) * modular_inverse(other.gamma_);
|
||||
}
|
||||
|
||||
splitmix64_base split() {
|
||||
uint64_t new_seed = operator()();
|
||||
uint64_t new_gamma = mix_gamma(next_seed());
|
||||
return { new_seed, new_gamma };
|
||||
}
|
||||
|
||||
bool operator==(const splitmix64_base& rhs) {
|
||||
return (seed_ == rhs.seed_) && (gamma_ == rhs.gamma_);
|
||||
}
|
||||
};
|
||||
|
||||
template <uint64_t m5, uint64_t m6, unsigned int v, unsigned int w,
|
||||
typename splitmix>
|
||||
class splitmix32_base : public splitmix {
|
||||
public:
|
||||
using result_type = uint32_t;
|
||||
static constexpr result_type min() { return 0; };
|
||||
static constexpr result_type max() { return ~result_type(0); };
|
||||
|
||||
using splitmix::splitmix;
|
||||
|
||||
result_type operator()() {
|
||||
uint64_t seed = splitmix::next_seed();
|
||||
seed ^= seed >> v;
|
||||
seed *= m5;
|
||||
seed ^= seed >> w;
|
||||
seed *= m6;
|
||||
return result_type(seed >> 32);
|
||||
}
|
||||
|
||||
splitmix32_base split() {
|
||||
return splitmix::split();
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
using splitmix64 = splitmix_detail::splitmix64_base<
|
||||
0xff51afd7ed558ccdul, 0xc4ceb9fe1a85ec53ul,
|
||||
33, 33, 33,
|
||||
0xbf58476d1ce4e5b9ul, 0x94d049bb133111ebul,
|
||||
30, 27, 31>;
|
||||
|
||||
using splitmix32 = splitmix_detail::splitmix32_base<
|
||||
0x62a9d9ed799705f5ul, 0xcb24d0a5c88c35b3ul,
|
||||
33, 28, splitmix64>;
|
||||
|
||||
#endif // SPLITMIX_HPP_INCLUDED
|
||||
45
test_scripts/pseudorandom_gens/splitmix32.c
Normal file
45
test_scripts/pseudorandom_gens/splitmix32.c
Normal file
@ -0,0 +1,45 @@
|
||||
/* SplitMix32 test program
|
||||
* Copyright (c) 2022 Joel K. Pettersson
|
||||
* <joelkpettersson@gmail.com>.
|
||||
*
|
||||
* Permission to use, copy, modify, and/or distribute this software for any
|
||||
* purpose with or without fee is hereby granted.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
#include "testwrite.h"
|
||||
#include "muvaror32.h"
|
||||
|
||||
/*
|
||||
* Here's SplitMix32 for comparison and a quick test.
|
||||
*/
|
||||
|
||||
//FIBH32 = 2654435769U
|
||||
|
||||
/**
|
||||
* Get next value for and update \p pos.
|
||||
*/
|
||||
static inline uint32_t splitmix32(uint32_t *restrict pos) {
|
||||
uint32_t s = *pos += FIBH32;
|
||||
s ^= s >> 16;
|
||||
s *= 0x85ebca6b;
|
||||
s ^= s >> 13;
|
||||
s *= 0xc2b2ae35;
|
||||
s ^= s >> 16;
|
||||
return s;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
uint32_t pos = 0;
|
||||
for (;;) {
|
||||
/* SplitMix32 test */
|
||||
add_output(splitmix32(&pos));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
108
test_scripts/pseudorandom_gens/xoroshiro128plus.c
Normal file
108
test_scripts/pseudorandom_gens/xoroshiro128plus.c
Normal file
@ -0,0 +1,108 @@
|
||||
/* Written in 2016-2018 by David Blackman and Sebastiano Vigna (vigna@acm.org)
|
||||
|
||||
To the extent possible under law, the author has dedicated all copyright
|
||||
and related and neighboring rights to this software to the public domain
|
||||
worldwide.
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
|
||||
IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/* This is xoroshiro128+ 1.0, our best and fastest small-state generator
|
||||
for floating-point numbers, but its state space is large enough only
|
||||
for mild parallelism. We suggest to use its upper bits for
|
||||
floating-point generation, as it is slightly faster than
|
||||
xoroshiro128++/xoroshiro128**. It passes all tests we are aware of
|
||||
except for the four lower bits, which might fail linearity tests (and
|
||||
just those), so if low linear complexity is not considered an issue (as
|
||||
it is usually the case) it can be used to generate 64-bit outputs, too;
|
||||
moreover, this generator has a very mild Hamming-weight dependency
|
||||
making our test (http://prng.di.unimi.it/hwd.php) fail after 5 TB of
|
||||
output; we believe this slight bias cannot affect any application. If
|
||||
you are concerned, use xoroshiro128++, xoroshiro128** or xoshiro256+.
|
||||
|
||||
We suggest to use a sign test to extract a random Boolean value, and
|
||||
right shifts to extract subsets of bits.
|
||||
|
||||
The state must be seeded so that it is not everywhere zero. If you have
|
||||
a 64-bit seed, we suggest to seed a splitmix64 generator and use its
|
||||
output to fill s.
|
||||
|
||||
NOTE: the parameters (a=24, b=16, b=37) of this version give slightly
|
||||
better results in our test than the 2016 version (a=55, b=14, c=36).
|
||||
*/
|
||||
|
||||
static inline uint64_t rotl(const uint64_t x, int k) {
|
||||
return (x << k) | (x >> (64 - k));
|
||||
}
|
||||
|
||||
|
||||
static uint64_t s[2];
|
||||
|
||||
uint64_t next(void) {
|
||||
const uint64_t s0 = s[0];
|
||||
uint64_t s1 = s[1];
|
||||
const uint64_t result = s0 + s1;
|
||||
|
||||
s1 ^= s0;
|
||||
s[0] = rotl(s0, 24) ^ s1 ^ (s1 << 16); // a, b
|
||||
s[1] = rotl(s1, 37); // c
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/* This is the jump function for the generator. It is equivalent
|
||||
to 2^64 calls to next(); it can be used to generate 2^64
|
||||
non-overlapping subsequences for parallel computations. */
|
||||
|
||||
void jump(void) {
|
||||
static const uint64_t JUMP[] = { 0xdf900294d8f554a5, 0x170865df4b3201fc };
|
||||
|
||||
uint64_t s0 = 0;
|
||||
uint64_t s1 = 0;
|
||||
for(int i = 0; i < sizeof JUMP / sizeof *JUMP; i++)
|
||||
for(int b = 0; b < 64; b++) {
|
||||
if (JUMP[i] & UINT64_C(1) << b) {
|
||||
s0 ^= s[0];
|
||||
s1 ^= s[1];
|
||||
}
|
||||
next();
|
||||
}
|
||||
|
||||
s[0] = s0;
|
||||
s[1] = s1;
|
||||
}
|
||||
|
||||
|
||||
/* This is the long-jump function for the generator. It is equivalent to
|
||||
2^96 calls to next(); it can be used to generate 2^32 starting points,
|
||||
from each of which jump() will generate 2^32 non-overlapping
|
||||
subsequences for parallel distributed computations. */
|
||||
|
||||
void long_jump(void) {
|
||||
static const uint64_t LONG_JUMP[] = { 0xd2a98b26625eee7b, 0xdddf9b1090aa7ac1 };
|
||||
|
||||
uint64_t s0 = 0;
|
||||
uint64_t s1 = 0;
|
||||
for(int i = 0; i < sizeof LONG_JUMP / sizeof *LONG_JUMP; i++)
|
||||
for(int b = 0; b < 64; b++) {
|
||||
if (LONG_JUMP[i] & UINT64_C(1) << b) {
|
||||
s0 ^= s[0];
|
||||
s1 ^= s[1];
|
||||
}
|
||||
next();
|
||||
}
|
||||
|
||||
s[0] = s0;
|
||||
s[1] = s1;
|
||||
}
|
||||
50
test_scripts/pseudorandom_gens/xoroshiro64starstar.c
Normal file
50
test_scripts/pseudorandom_gens/xoroshiro64starstar.c
Normal file
@ -0,0 +1,50 @@
|
||||
// https://prng.di.unimi.it/xoroshiro64starstar.c
|
||||
|
||||
/* Written in 2018 by David Blackman and Sebastiano Vigna (vigna@acm.org)
|
||||
|
||||
To the extent possible under law, the author has dedicated all copyright
|
||||
and related and neighboring rights to this software to the public domain
|
||||
worldwide.
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
|
||||
IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/* This is xoroshiro64** 1.0, our 32-bit all-purpose, rock-solid,
|
||||
small-state generator. It is extremely fast and it passes all tests we
|
||||
are aware of, but its state space is not large enough for any parallel
|
||||
application.
|
||||
|
||||
For generating just single-precision (i.e., 32-bit) floating-point
|
||||
numbers, xoroshiro64* is even faster.
|
||||
|
||||
The state must be seeded so that it is not everywhere zero. */
|
||||
|
||||
|
||||
static inline uint32_t rotl(const uint32_t x, int k) {
|
||||
return (x << k) | (x >> (32 - k));
|
||||
}
|
||||
|
||||
|
||||
static uint32_t s[2];
|
||||
|
||||
uint32_t next(void) {
|
||||
const uint32_t s0 = s[0];
|
||||
uint32_t s1 = s[1];
|
||||
const uint32_t result = rotl(s0 * 0x9E3779BB, 5) * 5;
|
||||
|
||||
s1 ^= s0;
|
||||
s[0] = rotl(s0, 26) ^ s1 ^ (s1 << 9); // a, b
|
||||
s[1] = rotl(s1, 13); // c
|
||||
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user