How to Control JavaScript Randomness: Seeded Random Numbers and V8 Implementation
This article explains how JavaScript's Math.random works, why it lacks a built‑in seed, and shows practical ways to override or seed the function—including V8's internal algorithm, simple custom implementations, and real‑world use cases for reproducible randomness in web games.
Background
CodeWars kata "Don't rely on luck" requires guessing a number that matches Math.floor(Math.random() * 100 + 1). The test fixture compares the guess with the generated random number, encouraging a solution that does not depend on luck.
Common cheat solutions
Because the test uses Math.random, participants often override built‑ins:
Math.random = function(){ return 0; };
var guess = 1; var guess = 10;
Math.floor = function(){ return guess; };Seeded random numbers in other languages
C provides srand(), Ruby seeds from system time, process ID and a counter. JavaScript lacks a comparable low‑level seed function.
V8 implementation of Math.random
V8 follows ECMA‑262 §15.8.2.14 and maintains an internal Uint32Array state. A simplified version of the algorithm is:
var rngstate; // Uint32Array initialized at startup
function MathRandom(){
var r0 = (MathImul(18030, rngstate[0] & 0xFFFF) + (rngstate[0] >>> 16)) | 0;
rngstate[0] = r0;
var r1 = (MathImul(36969, rngstate[1] & 0xFFFF) + (rngstate[1] >>> 16)) | 0;
rngstate[1] = r1;
var x = ((r0 << 16) + (r1 & 0xFFFF)) | 0;
return (x < 0 ? x + 0x100000000 : x) * 2.3283064365386962890625e-10;
}
function MathImul(x, y){ return %NumberImul(TO_NUMBER_INLINE(x), TO_NUMBER_INLINE(y)); }Simple custom seeded RNG
A quick deterministic replacement derives output from Math.sin:
Math.random = function(seed){
return ('0.' + Math.sin(seed).toString().substr(6));
};
// Example usage
Math.random(1); // → 0.709848078965Practical use case
In HTML5 mini‑games, storing the seed value allows the entire random scene (item drops, enemy spawns) to be regenerated after a pause without persisting every object.
seedrandom library
The widely adopted seedrandom library provides a seedable RNG for browsers and Node.js. Installation examples:
<script src="//cdnjs.cloudflare.com/ajax/libs/seedrandom/2.4.0/seedrandom.min.js"></script> $ bower install seedrandom $ npm install seedrandomTrue randomness vs. pseudo‑randomness
All the methods above generate pseudo‑random numbers, which are deterministic given the same seed. Genuine entropy can be obtained from external services such as random.org (atmospheric noise), HotBits (radioactive decay), or EntropyPool (multiple sources). A JavaScript wrapper for these services is available at https://github.com/RealRand/RealRand.
Conclusion
JavaScript does not expose a native seedable RNG. Developers can mimic V8’s internal algorithm, use a simple deterministic function, or rely on third‑party libraries like seedrandom. Controlling the seed enables reproducible randomness, which is essential for debugging, testing, and creating consistent game experiences.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Aotu Lab
Aotu Lab, founded in October 2015, is a front-end engineering team serving multi-platform products. The articles in this public account are intended to share and discuss technology, reflecting only the personal views of Aotu Lab members and not the official stance of JD.com Technology.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
