Created
October 23, 2020 07:29
-
-
Save TransparentLC/6deded2024808b48fef99b1a14abe81d to your computer and use it in GitHub Desktop.
可播种的随机数生成器 xoshiro128**
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
/** | |
* Random number generator "xoshiro128**". | |
* @see http://prng.di.unimi.it/xoshiro128starstar.c | |
*/ | |
class Xoshiro128ss { | |
/** | |
* Init the RNG with given seed. | |
* The default seed is taken from current timestamp. | |
* @param {Number} [a] | |
* @param {Number} [b] | |
* @param {Number} [c] | |
* @param {Number} [d] | |
*/ | |
constructor(a, b, c, d) { | |
a = a || (0x67452301 ^ Date.now()); | |
b = b || (0xEFCDAB89 ^ a); | |
c = c || (0x98BADCFE ^ b); | |
d = d || (0x10325476 ^ c) | |
this.$ = new Uint32Array([a, b, c, d]); | |
} | |
/** | |
* Get a random Uint32 number. | |
* @returns {Number} | |
*/ | |
randInt() { | |
const s = this.$; | |
const t = s[1] << 9; | |
const r = s[1] * 5; | |
s[2] ^= s[0]; | |
s[3] ^= s[1]; | |
s[1] ^= s[2]; | |
s[0] ^= s[3]; | |
s[2] ^= t; | |
s[3] = s[3] << 11 | s[3] >>> 21; | |
return (r << 7 | r >>> 25) * 9 >>> 0; | |
} | |
/** | |
* Get a random float number between 0 and 1. | |
* @returns {Number} | |
*/ | |
randFloat() { | |
return this.randInt() / 0xFFFFFFFF; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment