Last active
April 14, 2024 12:04
-
-
Save leomastoras/3f19ed365b821e4b3cd2c08c2980583c to your computer and use it in GitHub Desktop.
xorshift128+ random
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// what is randomness anyway | |
const RNG = { | |
random : () => RNG.xs128p(RNG.pair), | |
pair : [~-Math.E, ~-Math.PI], // [1,2] | |
xs128p : ([v, u]) => ( | |
( v ^= v << 23 ), // ~~(Math.E**Math.PI) | |
( v ^= v >> 17 ), // -~(Math.E<<Math.PI) | |
( v ^= u ), | |
( v ^= u >> 26 ), // ~~(Math.E**Math.PI+Math.E^Math.PI) | |
( RNG.pair = [u, v] ), | |
( u + v ) | |
) | |
}; | |
// 100 randoms | |
[...Array(100)].map(RNG.random) | |
const rng = (({ | |
E: e, | |
PI: π, | |
sin, abs | |
}) => ({ | |
seed : [ e , π ], | |
rand : (( [ v , u ] = rng.seed ) => ( | |
( v ^= v << e ** π | π ^ e ), | |
( v ^= v >> e << π | π ^ e ), | |
( v ^= u << π << e - e ^ π ), | |
( v ^= u >> π ** e + e ^ π ), | |
( rng.seed = [ u , v ] ), | |
( abs ( sin ( u + v ) )) | |
)), | |
}))(Math); | |
// 100 randoms | |
[...Array(100)].map(rng.rand); | |
const 𐑫 = (( { | |
E : ѵ, | |
PI : ⱱ, | |
sin: 𝗏, | |
abs: ꮩ, | |
} ) => ( { | |
ν : [ ѵ , ⱱ ], | |
rand : | |
(( [ v,ᴠ ] = ( 𐑫.ν ) )=>( | |
( v ^= v << ѵ ** ⱱ | ⱱ ^ ѵ ), | |
( v ^= v >> ѵ << ⱱ | ⱱ ^ ѵ ), | |
( v ^= ᴠ << ⱱ << ѵ - ѵ ^ ⱱ ), | |
( v ^= ᴠ >> ⱱ ** ѵ + ѵ ^ ⱱ ), | |
( ( 𐑫.ν ) = [ ᴠ,v ] ), | |
( ( ꮩ ( 𝗏 ( ᴠ ^ v ) ) ) ) ) ), | |
} )) ( Math ); | |
[...Array(100)].map(𐑫.rand); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment