Skip to content

Instantly share code, notes, and snippets.

@tommyettinger
Forked from zacharycarter/rng.nim
Last active October 3, 2017 05:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommyettinger/099243faf946a0c981964472c942d7d9 to your computer and use it in GitHub Desktop.
Save tommyettinger/099243faf946a0c981964472c942d7d9 to your computer and use it in GitHub Desktop.
#[ Written by Tommy Ettinger in 2017, based on work by Melissa O'Neill in 2015:
PCG-Random, http://www.pcg-random.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. This software is distributed without any warranty.
See <http://creativecommons.org/publicdomain/zero/1.0/>. ]#
var x*: uint64 # The state can be seeded with any value.
proc next*(): uint64 =
x += 0x9E3779B97F4A7C15u64
var z = (x xor (x shr (5u64 + (x shr 59)))) * 0xAEF17502108EF2D9u64;
z xor (z shr 43)
#[ Written in 2015 by 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. This software is distributed without any warranty.
See <http://creativecommons.org/publicdomain/zero/1.0/>. ]#
#[ This is a fixed-increment version of Java 8's SplittableRandom generator
See http://dx.doi.org/10.1145/2714064.2660195 and
http://docs.oracle.com/javase/8/docs/api/java/util/SplittableRandom.html
It is a very fast generator passing BigCrush, and it can be useful if
for some reason you absolutely want 64 bits of state; otherwise, we
rather suggest to use a xoroshiro128+ (for moderately parallel
computations) or xorshift1024* (for massively parallel computations)
generator. ]#
var x*: uint64 # The state can be seeded with any value.
proc next*(): uint64 =
x += 0x9E3779B97F4A7C15u64
var z = x
z = (z xor `shr`(z, 30)) * 0xBF58476D1CE4E5B9u64
z = (z xor `shr`(z, 27)) * 0x94D049BB133111EBu64
z xor `shr`(z, 31)
when isMainModule:
x = 0
assert next() == 16294208416658607535u64
x = 100
assert next() == 2532601429470541124u64
#[ Written by Tommy Ettinger in 2017
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. This software is distributed without any warranty.
See <http://creativecommons.org/publicdomain/zero/1.0/>. ]#
#[ This is ThrustRNG, a deceptively-small pseudo-random number generator
that passes difficult quality tests and is faster than PCG-Random
and SplitMix64 in most cases, while maintaining a period of 2 to the
64 and using the same general cipher-like structure as those two. ]#
var x*: uint64 # The state x can be seeded with any value.
proc next*(): uint64 =
x += 0x9E3779B97F4A7C15u64
var z = (x xor (x shr 30)) * 0x5851F42D4C957F2Du64
z xor (z shr 28)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment