HOWTO · JavaScript
How to Use a Seed to Generate Random Numbers in JavaScript
Learn how seeded pseudorandom generators turn numeric state into repeatable JavaScript sequences, convert strings into generator state, and use seedrandom.js.
On this page
To generate repeatable pseudorandom numbers in JavaScript, initialize a seeded generator and keep the seed, generator implementation, and call order the same each time. Math.random() does not accept a seed, so use a seeded PRNG such as the examples below or seedrandom.js.
How seeded randomness works
JavaScript’s Math.random() does not accept a seed. To reproduce a sequence, create a pseudorandom number generator (PRNG) that accepts initial state. Calling that generator with the same seed, the same implementation, and the same sequence of calls reproduces the sequence. Changing any of those inputs can change later values.
A seed initializes a generator; it is not itself a random number. Keep the conversion from user-friendly input, such as a string, separate from the algorithm that advances generator state. The examples below use small general-purpose PRNGs for repeatable simulations, demos, and tests.
Turn a string into generator seeds
SFC32 and Xoshiro128** use four 32-bit state words. A string therefore needs to be converted into four numeric values before it can initialize those generators. This compact hash is one way to do that; it is not a cryptographic hash and should not be used to protect secrets. Different strings can collide, as with any fixed-width hash.
The function mixes each UTF-16 code unit into four unsigned 32-bit words. Its output is suitable as deterministic example state, but it does not add entropy or make the generator secure. Use the same string and function whenever you need to recreate the same starting state.
function hashStringToWords(string) {
let a = 0x9e3779b9;
let b = 0x243f6a88;
let c = 0xb7e15162;
let d = 0xdeadbeef;
for (let i = 0; i < string.length; i++) {
const k = string.charCodeAt(i);
a = Math.imul(a ^ k, 0x85ebca6b);
b = Math.imul(b ^ k, 0xc2b2ae35);
c = Math.imul(c ^ k, 0x27d4eb2f);
d = Math.imul(d ^ k, 0x165667b1);
a ^= b >>> 13;
b ^= c >>> 11;
c ^= d >>> 15;
d ^= a >>> 9;
}
return [a, b, c, d].map((word) => word >>> 0);
}
const words = hashStringToWords("demo seed");
console.log(words.join(", "));
A generator must also handle its state requirements. SFC32 and Xoshiro128** take four words; Mulberry32 takes one 32-bit number; the JSF example below expands one word into four initial values and warms up the state before returning values. Do not pass the same single value four times and assume that every generator will initialize correctly.
Generate numbers with SFC32, Mulberry32, Xoshiro128**, and JSF
These functions return values in the range from 0 (inclusive) to 1 (exclusive). SFC32 and Xoshiro128** receive the four words from hashStringToWords; Mulberry32 and JSF use the first word. Each function closes over mutable state, so create a new generator to restart its sequence.
SFC32 advances four 32-bit words on each call, including when it starts from an all-zero state. Xoshiro128** requires a nonzero state; the hash helper supplies four nonzero words for the shown demo seed input.
function hashStringToWords(string) {
let a = 0x9e3779b9;
let b = 0x243f6a88;
let c = 0xb7e15162;
let d = 0xdeadbeef;
for (let i = 0; i < string.length; i++) {
const k = string.charCodeAt(i);
a = Math.imul(a ^ k, 0x85ebca6b);
b = Math.imul(b ^ k, 0xc2b2ae35);
c = Math.imul(c ^ k, 0x27d4eb2f);
d = Math.imul(d ^ k, 0x165667b1);
a ^= b >>> 13; b ^= c >>> 11; c ^= d >>> 15; d ^= a >>> 9;
}
return [a, b, c, d].map((word) => word >>> 0);
}
function sfc32(a, b, c, d) {
return function () {
a >>>= 0; b >>>= 0; c >>>= 0; d >>>= 0;
let t = (a + b) | 0;
a = b ^ (b >>> 9);
b = (c + (c << 3)) | 0;
c = (c << 21) | (c >>> 11);
d = (d + 1) | 0;
t = (t + d) | 0;
c = (c + t) | 0;
return (t >>> 0) / 4294967296;
};
}
const random = sfc32(...hashStringToWords("demo seed"));
console.log(random());
console.log(random());
Mulberry32 is a compact one-word generator. The addition by a fixed constant advances its state before mixing the bits into each result.
function mulberry32(seed) {
let state = seed >>> 0;
return function () {
let t = (state += 0x6d2b79f5);
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
function hashStringToWords(string) {
let a = 0x9e3779b9;
for (let i = 0; i < string.length; i++) {
a = Math.imul(a ^ string.charCodeAt(i), 0x85ebca6b);
a ^= a >>> 13;
}
return a >>> 0;
}
const random = mulberry32(hashStringToWords("demo seed"));
for (let i = 0; i < 5; i++) console.log(random());
Xoshiro128** uses four words and a star-star output transform. Its state transition is separate from the output calculation, so preserve all four updated words between calls.
function xoshiro128ss(s) {
s = s.map((word) => word >>> 0);
return function () {
const result = Math.imul(((Math.imul(s[1], 5) << 7) |
(Math.imul(s[1], 5) >>> 25)), 9) >>> 0;
const t = s[1] << 9;
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 result / 4294967296;
};
}
function hashStringToWords(string) {
let a = 0x9e3779b9, b = 0x243f6a88, c = 0xb7e15162, d = 0xdeadbeef;
for (let i = 0; i < string.length; i++) {
const k = string.charCodeAt(i);
a = Math.imul(a ^ k, 0x85ebca6b); b = Math.imul(b ^ k, 0xc2b2ae35);
c = Math.imul(c ^ k, 0x27d4eb2f); d = Math.imul(d ^ k, 0x165667b1);
a ^= b >>> 13; b ^= c >>> 11; c ^= d >>> 15; d ^= a >>> 9;
}
return [a, b, c, d].map((word) => word >>> 0);
}
const random = xoshiro128ss(hashStringToWords("demo seed"));
console.log(random());
JSF (Jenkins Small Fast) also keeps four words. This version derives the initial state from the first hash word, performs 20 state transitions before producing values, then returns a 32-bit result scaled to the same interval.
function jsf32(seed) {
let a = 0xf1ea5eed;
let b = seed >>> 0;
let c = seed >>> 0;
let d = seed >>> 0;
function nextUint32() {
const e = (a - ((b << 27) | (b >>> 5))) | 0;
a = b ^ ((c << 17) | (c >>> 15));
b = (c + d) | 0;
c = (d + e) | 0;
d = (a + e) | 0;
return d >>> 0;
}
for (let i = 0; i < 20; i++) nextUint32();
return () => nextUint32() / 4294967296;
}
function hashStringToWord(string) {
let a = 0x9e3779b9;
for (let i = 0; i < string.length; i++) {
a = Math.imul(a ^ string.charCodeAt(i), 0x85ebca6b);
a ^= a >>> 13;
}
return a >>> 0;
}
const random = jsf32(hashStringToWord("demo seed"));
for (let i = 0; i < 3; i++) console.log(random());
The algorithms produce different sequences even when initialized from the same string because their state transitions differ. Reproducibility requires keeping the algorithm and call order fixed as well as the seed.
Use seedrandom.js
seedrandom.js is a JavaScript library for seeded pseudorandom generation. The following browser-page source loads version 3.0.5 from CDNJS, creates a generator from the string Johnson, and logs its first value. The snippet requires a browser with network access to load the script; it is source to adapt, not a runtime-verified output here.
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/seedrandom/3.0.5/seedrandom.min.js"></script>
<script>
const random = new Math.seedrandom("Johnson");
console.log(random());
</script>
</body>
A generator instance also provides quick() and int32() methods. quick() returns a fractional value; int32() returns a signed 32-bit integer. Each call advances the generator, so the second call does not represent the same point in its sequence as the first.
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/seedrandom/3.0.5/seedrandom.min.js"></script>
<script>
const random = new Math.seedrandom("DavidBau");
console.log("With quick():", random.quick());
console.log("With int32():", random.int32());
</script>
</body>
Choose seeded, entropy-mixed, or automatic behavior
Use an explicit seed when you need a sequence that can be recreated from the same input. The supplied seedrandom.js example also shows an { entropy: true } option alongside a seed. That example demonstrates passing the option; it does not establish a particular entropy source, guarantee, or repeatability property, so do not rely on it for those claims.
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/seedrandom/3.0.5/seedrandom.min.js"></script>
<script>
const random = new Math.seedrandom("Antananarivo", { entropy: true });
for (let i = 0; i < 5; i++) console.log(random());
</script>
</body>
If you need fresh values instead of a repeatable demonstration, choose an appropriate source for that requirement and consult its documented behavior. This article does not assume behavior for omitted seed arguments or options beyond the calls shown above.
Limits of seeded pseudorandom numbers
Seeded PRNGs are useful when a program needs repeatable sequences, such as a simulation or a deterministic test fixture. They are algorithms, not sources of unpredictable secrets. A predictable seed makes the resulting sequence predictable, and a custom string hash does not change that fact.
Do not use these examples to generate passwords, authentication tokens, cryptographic keys, or other security-sensitive values. For those tasks, use a cryptographically secure randomness API appropriate to the runtime and platform. Also avoid treating a seed as a guarantee that two different PRNG implementations will agree: preserve the algorithm, seed conversion, initial state, and number and order of calls.