Code Monkey home page Code Monkey logo

random-js's Introduction

Original source of MT implementation unknown... Just found it on my drive without attribution. Cleaned it up a little and wrote a wrapper to emulate (and then some) Math.random.

random-js's People

Contributors

ryantenney avatar

Stargazers

 avatar

Watchers

 avatar  avatar

random-js's Issues

Multiplication overflow in initializeGenerator

This line implements the multiplication incorrectly:

MT[i] = (0x6c078965 * (MT[i-1] ^ (MT[i] >> 30)) + i) & 0xffffffff;

JavaScript loses precision with more than 53 bits, so the multiplication must be split into two multiplications, for the upper and lower 16 bits. This is what the other (correct) JavaScript implementations do:

https://github.com/boo1ean/mersenne-twister/blob/bdb5bd16d571c527d88ec29262ac3dde13798da7/src/mersenne-twister.js#L96-L97

this.mt[this.mti] = (((((s & 0xffff0000) >>> 16) * 1812433253) << 16) + (s & 0x0000ffff) * 1812433253)
+ this.mti;

https://github.com/dsibilly/mersenne-twister/blob/aecd790b2493c2e6f7435b8351b56f47a3a64935/js/mersenne-twister.js#L77-L78

this.state[this.stateIndex] = (((((s & 0xffff0000) >>> 16) * 1812433253) << 16) + (s & 0x0000ffff) * 1812433253) + this.stateIndex;
this.state[this.stateIndex] >>>= 0;

https://gitlab.com/rockerest/fast-mersenne-twister/-/blob/bc11ee4d3f6d550d7548d4842f45b8d66fc8a7f2/mersenne.js#L101-111

// avoid multiplication overflow: split 32 bits into 2x 16 bits and process them individually

state[i]  = (
	(
		(
			(
				( s & 0xffff0000 ) >>> 16
			) * 1812433253
		) << 16
	) + ( s & 0x0000ffff ) * 1812433253
) + i;

https://github.com/pigulla/mersennetwister/blob/5a747d99ef0831e5d1ffddfdbb6ea70f539501d4/src/MersenneTwister.js#L70-L72

this.mt[this.mti] =
    (((((s & 0xffff0000) >>> 16) * 1812433253) << 16) + (s & 0x0000ffff) * 1812433253) + this.mti;
this.mt[this.mti] >>>= 0;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.