Created
January 29, 2020 19:12
-
-
Save morbidcamel101/62477623c4d2e04f68c2615d0dcc386b to your computer and use it in GitHub Desktop.
Simple random number generator. We don't need great randomness, we just need a little and for it to be fast.
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
internal struct FastRandom // xorshift prng | |
{ | |
private uint _w, _x, _y, _z; | |
public FastRandom(int seed) | |
{ | |
_x = (uint)seed; | |
_w = 88675123; | |
_y = 362436069; | |
_z = 521288629; | |
} | |
public int Next(int maxValue) | |
{ | |
Debug.Assert(maxValue > 0); | |
uint t = _x ^ (_x << 11); | |
_x = _y; _y = _z; _z = _w; | |
_w = _w ^ (_w >> 19) ^ (t ^ (t >> 8)); | |
return (int)(_w % (uint)maxValue); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment