Instantly share code, notes, and snippets.

What would you like to do?
A very simple, seedable JavaScript PRNG.
* Creates a pseudo-random value generator. The seed must be an integer.
* Uses an optimized version of the Park-Miller PRNG.
function Random(seed) {
this._seed = seed % 2147483647;
if (this._seed <= 0) this._seed += 2147483646;
* Returns a pseudo-random value between 1 and 2^32 - 2.
*/ = function () {
return this._seed = this._seed * 16807 % 2147483647;
* Returns a pseudo-random floating point number in range [0, 1).
Random.prototype.nextFloat = function (opt_minOrMax, opt_max) {
// We know that result of next() will be 1 to 2147483646 (inclusive).
return ( - 1) / 2147483646;

This comment has been minimized.

madole commented Jul 30, 2014

You dont check that the seed is a number before performing arithmetic calculations on it. potential NaN returned.


This comment has been minimized.

SpaceUnicorn commented Mar 22, 2017

Didn't think it would be this simple, but I guess it is! I wrote a graphic representation of the distribution here:
A couple thousand runs show that it's pretty much uniform. Nice work 👍


This comment has been minimized.

rayfoss commented Aug 24, 2017

Unlike the Math.sin versions of this, this will produce the same result on Node, Chrome, Safari and Firefox... meaning I can save the seed and regenerate the same results across environments.


This comment has been minimized.

momander commented Aug 28, 2017

Very nice! This is just what I needed for my multi-player game, where some random events need to generated on all clients at the same time. With this generator all clients will get the same "random" values, without having to talk over the network.


This comment has been minimized.

TBubba commented Sep 19, 2017

Well done! Also, thanks @rayfoss for pointing out that it works across platforms, because that's exactly what I was looking for! 👍


This comment has been minimized.

tommitytom commented Oct 16, 2017

Do bear in mind that the float version will be affected by floating point rounding errors in different ways on different platforms - so if you want the same numbers on all platforms make sure you stick to using the integer version!


This comment has been minimized.

AmaanC commented Dec 1, 2017

Ended up modifying it a little to return random 32-bit signed ints, so I plotted a histogram and a random bitmap to verify the distribution was decent.




This comment has been minimized.

ghost commented Dec 19, 2017

Hehe. Talk about an obscure next() algorithm!

It relies on this._seed * 16807 % 2147483647 never becoming "0". If that happens, then all next() calls after that will return 0.

In other words, 1st part (this._seed * 16807) is never allowed to become 2147483647.

Luckily, the number is well-chosen. Because to get 2147483647 % 2147483647 == 0, the 1st part of the equation would have to become 2147483647. So to find out what that number would have to be, we just check 2147483647 / 16807 = 127773.168739216. So we see that we need a very specific, float number for the next() algorithm to bug out.

Luckily, as long as you seed this class with an integer, then the seed * 16807 will never produce a float and can never bug out.

Fascinating algorithm. Thank you...

PS: Your code has some funny variables: Random.prototype.nextFloat = function (opt_minOrMax, opt_max).


This comment has been minimized.

backspaces commented Jan 21, 2018

Way cool, thanks! Doesn't repeat itself for quite a while. The sin prng seems to repeat in the low millions.

This is a simpler version for floats only. It returns the PRNG function:

  function randomSeedParkMiller (seed = 123456) { // doesn't repeat b4 JS dies.
    seed = seed % 2147483647
    return () => {
      seed = seed * 16807 % 2147483647
      return (seed - 1) / 2147483646

Here's the sin version:

  function randomSeedSin (seed = Math.PI / 4) { // ~3.4 million b4 repeat.
    return () => {
      const x = Math.sin(seed++) * 10000
      return x - Math.floor(x)

It's written to return the function so that it can be used to replace Math.random:

Math.random = randomSeedParkMiller(seed)

This comment has been minimized.

santinod commented Jan 30, 2018

With the current code, initialization fails if seed = -2147483646. My suggestion:

function Random(seed) {
  this._seed = Math.abs(seed % 2147483646) + 1;

This comment has been minimized.

juliango202 commented Mar 21, 2018

Thanks! There is a typo in the comment for the next() method, the upper limit is 2^31 - 2.


This comment has been minimized.

bungu commented Jul 9, 2018

It's possible to use this function as one-liner:

const prng = s => (typeof s!=='undefined'&&((l=s%2147483647)<=0&&(l+=2147483646)),((l=l*16807%2147483647)-1)/2147483646);
const seed = 3;

console.log(prng(seed));		// 0.000023478642127922403
console.log(prng());		// 0.3946133641475936
console.log(prng());		// 0.26681596624368425
console.log(prng());		// 0.3759503954797521
console.log(prng());		// 0.5983017120494524

Just call it again with seed to start new sequence.


This comment has been minimized.

WalterBarrett commented Jul 25, 2018

What's the license for this code?


This comment has been minimized.

bryc commented Aug 18, 2018

That's a Lehmer LCG designed from 1988, simple but effective. Park/Miller proposed using 48271 instead of 16807 in response to a criticism by Marsaglia in 1993.

Here's a fast implementation that uses the new multiplier instead (license=CC0):

// 1993 Park-Miller LCG
function LCG(s) {
    return function() {
      s = Math.imul(16807, s) | 0 % 2147483647;
      return (s & 2147483647) / 2147483648;

// Use it like so:
var rand = LCG(123);
rand(); // returns 0.45899124443531036
rand(); // returns 0.9663704535923898

Also @bungu.. that oneliner is nice but it puts l in the global scope, bad bad! Might as well do this:

var s=3,rnd=a=>(s=s*16807%(2**31-1))/2**31

// or my faster version above, minified:
var LCG=s=>()=>(2**31-1&(s=Math.imul(48271,s)))/2**31

Even shorter 😄

Also why not look at some other 32-bit oneliners?

// a 32-bit LCG (rather than 31-bit one above)
var LCG=s=>()=>((s=Math.imul(741103597,s))>>>0)/2**32;

// original xorshift32
var xs32=s=>()=>((s^=s<<25,s^=s>>>7,s^=s<<2)>>>0)/2**32;

// xorshift32 with knuth multiplicative
var xs32=s=>()=>(Math.imul(1597334677,s^=s<<25,s^=s>>>7,s^=s<<2)>>>0)/2**32;

// mulberry32 - an actual high quality 32-bit generator
var mb32=a=>(t)=>(a=a+1831565813|0,t=Math.imul(a^a>>>15,1|a),t=t+Math.imul(t^t>>>7,61|t)^t,(t^t>>>14)>>>0)/2**32;

This comment has been minimized.

candy0414 commented Dec 5, 2018

i like it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment