Is it possible to seed the random number generator (Math.random) in JavaScript?
-
4it is not clear whether you want to seed it so that you get the same results repeatedly for different test runs or whether you want to seed it with 'something unique' per user for better randomness between usage.simbo1905– simbo19052014-05-16 05:45:52 +00:00Commented May 16, 2014 at 5:45
-
6No, unfortunately it is not possible. jsrand is a little library I wrote when I needed a seedable PRNG. There are also other more complex libraries that you can find googling for it.Domenico De Felice– Domenico De Felice2015-01-02 18:01:34 +00:00Commented Jan 2, 2015 at 18:01
-
21Adding to the question: how is it possibly a good idea to offer a PRNG without a means to seed it?? Is there any good reason for this?Alan– Alan2018-07-18 17:58:30 +00:00Commented Jul 18, 2018 at 17:58
-
5Here's a visualization of some of the generators on this page observablehq.com/@tidwall/hello-randomnesstidwall– tidwall2020-09-15 18:39:41 +00:00Commented Sep 15, 2020 at 18:39
-
8@Alan I think maybe there's no seed because the underlying algorithm is up to the browser - if Math.random() did have a seed, the seeds would not be guaranteed to give the same results in different browsers. hackernoon.com/…codeulike– codeulike2021-06-02 08:44:52 +00:00Commented Jun 2, 2021 at 8:44
23 Answers
No, it is not possible to seed Math.random(). The ECMAScript specification is intentionally vague on the subject, providing no means for seeding nor require that browsers even use the same algorithm. So such a function must be externally provided, which thankfully isn't too difficult.
I've implemented a number of good, short and fast Pseudorandom number generator (PRNG) functions in plain JavaScript. All of them can be seeded and provide high quality numbers. These are not intended for security purposes--if you need a seedable CSPRNG, look into ISAAC.
First of all, take care to initialize your PRNGs properly. Improper use of seeds in a bare PRNG can lead to poor randomness in the first few numbers. To keep things simple, the generators below have no built-in seed generating procedure, but simply accept one or more 32-bit numbers as the initial seed state of the PRNG. Similar or sparse seeds (e.g. a simple seed of 1 and 2) have low entropy, and can cause correlations or other randomness quality issues, sometimes resulting in the output having similar properties (such as randomly generated levels being similar). To avoid this, it is best practice to initialize PRNGs with a well-distributed, high entropy seed and/or advancing past the first 15 or so numbers.
There are many ways to do this, but here are two methods. Firstly, hash functions are very good at generating seeds from short strings. A good hash function will generate very different results even when two input strings are similar, so you don't have to put much thought into the string. Here's an example hash function:
function cyrb128(str) {
let h1 = 1779033703, h2 = 3144134277,
h3 = 1013904242, h4 = 2773480762;
for (let i = 0, k; i < str.length; i++) {
k = str.charCodeAt(i);
h1 = h2 ^ Math.imul(h1 ^ k, 597399067);
h2 = h3 ^ Math.imul(h2 ^ k, 2869860233);
h3 = h4 ^ Math.imul(h3 ^ k, 951274213);
h4 = h1 ^ Math.imul(h4 ^ k, 2716044179);
}
h1 = Math.imul(h3 ^ (h1 >>> 18), 597399067);
h2 = Math.imul(h4 ^ (h2 >>> 22), 2869860233);
h3 = Math.imul(h1 ^ (h3 >>> 17), 951274213);
h4 = Math.imul(h2 ^ (h4 >>> 19), 2716044179);
h1 ^= (h2 ^ h3 ^ h4), h2 ^= h1, h3 ^= h1, h4 ^= h1;
return [h1>>>0, h2>>>0, h3>>>0, h4>>>0];
}
// Side note: Only designed & tested for seed generation,
// may be suboptimal as a general 128-bit hash.
Calling cyrb128 will produce a 128-bit hash value from a string which can be used to seed a PRNG. Here's how you might use it:
// Create cyrb128 state:
var seed = cyrb128("apples");
// Four 32-bit component hashes provide the seed for sfc32.
var rand = sfc32(seed[0], seed[1], seed[2], seed[3]);
// Or... only one 32-bit component hash is needed for splitmix32.
var rand = splitmix32(seed[0]);
// You can now generate repeatable sequences of random numbers:
rand(); // 0.8865117691457272
rand(); // 0.24518639338202775
Note: If you want a slightly more robust 128-bit hash, consider MurmurHash3_x86_128, it's more thorough, but intended for use with large arrays. Since this operates on byte arrays, strings require conversion using something like Array.from("hello", c=>c.charCodeAt()).
Alternatively, simply choose some dummy data to pad the seed with, and advance the generator beforehand a few times (12-20 iterations) to mix the initial state thoroughly. This has the benefit of being simpler, and is often used in reference implementations of PRNGs, but it does limit the number of initial states:
var seed = 1337 ^ 0xDEADBEEF; // 32-bit seed with optional XOR value
// Pad seed with Phi, Pi and E.
// https://en.wikipedia.org/wiki/Nothing-up-my-sleeve_number
var rand = sfc32(0x9E3779B9, 0x243F6A88, 0xB7E15162, seed);
for (var i = 0; i < 15; i++) rand();
Note: the output of these PRNG functions produce a positive 32-bit number (0 to 232-1) which is then converted to a floating-point number between 0-1 (0 inclusive, 1 exclusive) equivalent to Math.random(), if you want random numbers of a specific range, read this article on MDN. If you only want the raw bits, simply remove the final division operation.
JavaScript numbers can only represent whole integers up to 53-bit resolution. And when using bitwise operations, this is reduced to 32. Modern PRNGs in other languages often use 64-bit operations, which require shims when porting to JS that can drastically reduce performance. The algorithms here only use 32-bit operations, as it is directly compatible with JS (and thus performant). Now, onward to the the generators.
(I maintain the full list with references and license info here)
sfc32 (Simple Fast Counter)
sfc32 is part of the PractRand random number testing suite (which it passes of course). sfc32 has a 128-bit state and is very fast in JS.
function sfc32(a, b, c, d) {
return function() {
a |= 0; b |= 0; c |= 0; d |= 0;
let t = (a + b | 0) + d | 0;
d = d + 1 | 0;
a = b ^ b >>> 9;
b = c + (c << 3) | 0;
c = (c << 21 | c >>> 11);
c = c + t | 0;
return (t >>> 0) / 4294967296;
}
}
const seedgen = () => (Math.random()*2**32)>>>0;
const getRand = sfc32(seedgen(), seedgen(), seedgen(), seedgen());
for(let i=0; i<10; i++) console.log(getRand());
Side note: You may wonder what the
| 0and|= 0are for. These are essentially 32-bit integer casts, used for performance optimizations.Numberin JS are basically floats, but during bitwise operations, they switch into a 32-bit integer mode. This mode is processed faster by JS engines, but any multiplication or addition will cause it to switch back to a float, resulting in a performance hit.
SplitMix32
A 32-bit state PRNG that was made by taking MurmurHash3's mixing function, adding a incrementor and tweaking the constants. It's potentially one of the better 32-bit PRNGs so far; even the author of Mulberry32 considers it to be the better choice. It's also just as fast.
function splitmix32(a) {
return function() {
a |= 0;
a = a + 0x9e3779b9 | 0;
let t = a ^ a >>> 16;
t = Math.imul(t, 0x21f0aaad);
t = t ^ t >>> 15;
t = Math.imul(t, 0x735a2d97);
return ((t = t ^ t >>> 15) >>> 0) / 4294967296;
}
}
const prng = splitmix32((Math.random()*2**32)>>>0)
for(let i=0; i<10; i++) console.log(prng());
I would recommend this if you just need a simple but good PRNG and don't need billions of random numbers (see Birthday problem).
Note: It does have one potential concern: it does not repeat previous numbers until you exhaust 4.3 billion numbers and it repeats again. Which may or may not be a statistical concern for your use case. It's like a list of random numbers with the duplicates removed, but without any extra work involved to remove them. All other generators in this list do not exhibit this behavior.
Mulberry32
Mulberry32 is a simple generator with a 32-bit state, but is extremely fast and has acceptable quality randomness (author states it passes all tests of gjrand testing suite).
function mulberry32(a) {
return function() {
let t = a += 0x6D2B79F5;
t = Math.imul(t ^ t >>> 15, t | 1);
t ^= t + Math.imul(t ^ t >>> 7, t | 61);
return ((t ^ t >>> 14) >>> 0) / 4294967296;
}
}
const getRand = mulberry32((Math.random()*2**32)>>>0)
for(let i=0; i<10; i++) console.log(getRand());
xoshiro128**
xoshiro128** (Vigna & Blackman, 2018) is part of the Xorshift lineage (Vigna was also responsible for the Xorshift128+ algorithm powering most Math.random implementations under the hood). It is the fastest generator that offers a 128-bit state.
function xoshiro128ss(a, b, c, d) {
return function() {
let t = b << 9, r = b * 5;
r = (r << 7 | r >>> 25) * 9;
c ^= a;
d ^= b;
b ^= c;
a ^= d;
c ^= t;
d = d << 11 | d >>> 21;
return (r >>> 0) / 4294967296;
}
}
const seedgen = () => (Math.random()*2**32)>>>0;
const getRand = xoshiro128ss(seedgen(), seedgen(), seedgen(), seedgen());
for (let i = 0; i < 10; i++) console.log(getRand());
The authors claim it passes randomness tests well (albeit with caveats). Other researchers have pointed out that it fails some tests in TestU01 (particularly LinearComp and BinaryRank). In practice, it should not cause issues when floats are used (such as in these implementations), but may cause issues if relying on the raw lowest order bit.
JSF (Jenkins' Small Fast)
This is JSF or 'smallprng' by Bob Jenkins (2007), who also made ISAAC and SpookyHash. It passes PractRand tests and should be quite fast, although not as fast as sfc32.
function jsf32(a, b, c, d) {
return function() {
a |= 0; b |= 0; c |= 0; d |= 0;
let t = a - (b << 27 | b >>> 5) | 0;
a = b ^ (c << 17 | c >>> 15);
b = c + d | 0;
c = d + t | 0;
d = a + t | 0;
return (d >>> 0) / 4294967296;
}
}
const seedgen = () => (Math.random()*2**32)>>>0;
const getRand = jsf32(seedgen(), seedgen(), seedgen(), seedgen());
for (let i = 0; i < 10; i++) console.log(getRand());
29 Comments
| 0, in firefox: jsbench.me/afk9jv36oz/1.No, it is not possible to seed Math.random(), but it's fairly easy to write your own generator, or better yet, use an existing one.
Check out: this related question.
Also, see David Bau's blog for more information on seeding.
Comments
NOTE: Despite (or rather, because of) succinctness and apparent elegance, this algorithm is by no means a high-quality one in terms of randomness. Look for e.g. those listed in this answer for better results.
(Originally adapted from a clever idea presented in a comment to another answer.)
var seed = 1;
function random() {
var x = Math.sin(seed++) * 10000;
return x - Math.floor(x);
}
You can set seed to be any number, just avoid zero (or any multiple of Math.PI).
The elegance of this solution, in my opinion, comes from the lack of any "magic" numbers (besides 10000, which represents about the minimum amount of digits you must throw away to avoid odd patterns - see results with values 10, 100, 1000). Brevity is also nice.
It's a bit slower than Math.random() (by a factor of 2 or 3), but I believe it's about as fast as any other solution written in JavaScript.
17 Comments
No, but here's a simple pseudorandom generator, an implementation of Multiply-with-carry I adapted from Wikipedia (has been removed since):
var m_w = 123456789;
var m_z = 987654321;
var mask = 0xffffffff;
// Takes any integer
function seed(i) {
m_w = (123456789 + i) & mask;
m_z = (987654321 - i) & mask;
}
// Returns number between 0 (inclusive) and 1.0 (exclusive),
// just like Math.random().
function random()
{
m_z = (36969 * (m_z & 65535) + (m_z >> 16)) & mask;
m_w = (18000 * (m_w & 65535) + (m_w >> 16)) & mask;
var result = ((m_z << 16) + (m_w & 65535)) >>> 0;
result /= 4294967296;
return result;
}
9 Comments
m_w, not m_z. 2) Both m_w and m_z are change BASED on their previous values, so it does modify the result.Antti Sykäri's algorithm is nice and short. I initially made a variation that replaced JavaScript's Math.random when you call Math.seed(s), but then Jason commented that returning the function would be better:
Math.seed = function(s) {
return function() {
s = Math.sin(s) * 10000; return s - Math.floor(s);
};
};
// usage:
var random1 = Math.seed(42);
var random2 = Math.seed(random1());
Math.random = Math.seed(random2());
This gives you another functionality that JavaScript doesn't have: multiple independent random generators. That is especially important if you want to have multiple repeatable simulations running at the same time.
8 Comments
Math.random that would allow you to have multiple independent generators, right?Math.seed(42); it resets the function, so if you do var random = Math.seed(42); random(); random(); you get 0.70..., then 0.38.... If you reset it by calling var random = Math.seed(42); again, then the next time you call random() you'll get 0.70... again, and the next time you'll get 0.38... again.random instead of overwriting a native javascript function. Overwriting Math.random may cause the JIST compiler to unoptimize all your code.It's not possible to seed the builtin Math.random function, but it is possible to implement a high quality RNG in Javascript with very little code.
Javascript numbers are 64-bit floating point precision, which can represent all positive integers less than 2^53. This puts a hard limit to our arithmetic, but within these limits you can still pick parameters for a high quality Lehmer / LCG random number generator.
function RNG(seed) {
var m = 2**35 - 31
var a = 185852
var s = seed % m
return function () {
return (s = s * a % m) / m
}
}
Math.random = RNG(Date.now())
If you want even higher quality random numbers, at the cost of being ~10 times slower, you can use BigInt for the arithmetic and pick parameters where m is just able to fit in a double.
function RNG(seed) {
var m_as_number = 2**53 - 111
var m = 2n**53n - 111n
var a = 5667072534355537n
var s = BigInt(seed) % m
return function () {
return Number(s = s * a % m) / m_as_number
}
}
See this paper by Pierre l'Ecuyer for the parameters used in the above implementations: https://www.ams.org/journals/mcom/1999-68-225/S0025-5718-99-00996-5/S0025-5718-99-00996-5.pdf
And whatever you do, avoid all the other answers here that use Math.sin!
2 Comments
Please see Pierre L'Ecuyer's work going back to the late 1980s and early 1990s. There are others as well. Creating a (pseudo) random number generator on your own, if you are not an expert, is pretty dangerous, because there is a high likelihood of either the results not being statistically random or in having a small period. Pierre (and others) have put together some good (pseudo) random number generators that are easy to implement. I use one of his LFSR generators.
https://www.iro.umontreal.ca/~lecuyer/myftp/papers/handstat.pdf
2 Comments
Combining some of the previous answers, this is the seedable random function you are looking for:
Math.seed = function(s) {
var mask = 0xffffffff;
var m_w = (123456789 + s) & mask;
var m_z = (987654321 - s) & mask;
return function() {
m_z = (36969 * (m_z & 65535) + (m_z >>> 16)) & mask;
m_w = (18000 * (m_w & 65535) + (m_w >>> 16)) & mask;
var result = ((m_z << 16) + (m_w & 65535)) >>> 0;
result /= 4294967296;
return result;
}
}
var myRandomFunction = Math.seed(1234);
var randomNumber = myRandomFunction();
5 Comments
Math.seed(0)() returns 0.2322845458984375, and Math.seed(1)() returns 0.23228873685002327. Changing both m_w and m_z according to the seed seems to help. var m_w = 987654321 + s; var m_z = 123456789 - s; produces a nice distribution of first values with different seeds.>>> 0 and why it's better than the more straightforward & mask.>>> 0 is a special operator used to obtain an unsigned representation of a 32-bit number, which is often necessary to match an algorithm that expects unsigned numbers. AND cannot achieve this, unfortunately.To write your own pseudo random generator is quite simple.
The suggestion of Dave Scotese is useful but, as pointed out by others, it is not quite uniformly distributed.
However, it is not because of the integer arguments of sin. It's simply because of the range of sin, which happens to be a one dimensional projection of a circle. If you would take the angle of the circle instead it would be uniform.
So instead of sin(x) use arg(exp(i * x)) / (2 * PI).
If you don't like the linear order, mix it a bit up with xor. The actual factor doesn't matter that much either.
To generate n pseudo random numbers one could use the code:
function psora(k, n) {
var r = Math.PI * (k ^ n)
return r - Math.floor(r)
}
n = 42; for(k = 0; k < n; k++) console.log(psora(k, n))
Please also note that you cannot use pseudo random sequences when real entropy is needed.
2 Comments
Many people who need a seedable random-number generator in Javascript these days are using David Bau's seedrandom module.
Comments
Math.random no, but the ran library solves this. It has almost all distributions you can imagine and supports seeded random number generation. Example:
ran.core.seed(0)
myDist = new ran.Dist.Uniform(0, 1)
samples = myDist.sample(1000)
Comments
After an enlightening discussion with two of my former maths teachers, I reworked Antti Kissaniemi's answer for the joke. At first, of course, they cried out in distress. Then they made the effort to try and understand to explain to me the bias towards 0 and 1 in the original answer.
Then they told me that they "see no reason why the original proposal would be interesting. In this case, sine exactly breaks the property we want to obtain: it destroys uniformity and that's why you have to multiply by 10000 afterwards".
They proposed a same-level solution: using an integer directly, divide it by PI, and return the fractional part.
let seed = 1;
function random() {
let x = seed++ / Math.PI;
return x - Math.floor(x);
}
And it seems to be generally slightly more performant, without the bias, still uniform. https://jsperf.app/xoxato
Testing
To add some seriousness to the joke, we can use PractRand with M.E. O'Neill tips to test our seeded generator. Using this C++ snippet:
#include <cstdio>
#include <cstdint>
#include <cmath>
#include <limits>
# define M_PI 3.14159265358979323846
// ECMAScript 2024 Math.PI value is approximately 3.1415926535897932
uint32_t seed = 1;
double rdm() {
double x = seed++ / M_PI;
return x - std::floor(x);
}
int main() {
while (1) {
double randomValue = rdm() * static_cast<double>(std::numeric_limits<uint32_t>::max());
uint32_t value = static_cast<uint32_t>(randomValue);
fwrite((void*)&value, sizeof(value), 1, stdout);
}
}
Then follow the blog post instructions:
$ ./pitest | ./RNG_test stdin32
RNG_test using PractRand version 0.93
RNG = RNG_stdin32, seed = 0xcdbb7f8d
test set = normal, folding = standard (32 bit)
rng=RNG_stdin32, seed=0xcdbb7f8d
length= 256 megabytes (2^28 bytes), time= 3.5 seconds
Test Name Raw Processed Evaluation
BCFN(2+0,13-2,T) R=+2083708 p = 0 FAIL !!!!!!!!
BCFN(2+1,13-2,T) R=+1922191 p = 0 FAIL !!!!!!!!
BCFN(2+2,13-3,T) R=+2225045 p = 0 FAIL !!!!!!!!
BCFN(2+3,13-3,T) R=+1873278 p = 0 FAIL !!!!!!!!
BCFN(2+4,13-3,T) R=+1254072 p = 0 FAIL !!!!!!!!
BCFN(2+5,13-4,T) R=+925525 p = 0 FAIL !!!!!!!!
BCFN(2+6,13-5,T) R=+599006 p = 0 FAIL !!!!!!!!
BCFN(2+7,13-5,T) R=+303954 p = 0 FAIL !!!!!!!!
BCFN(2+8,13-6,T) R=+189477 p = 0 FAIL !!!!!!!!
BCFN(2+9,13-6,T) R=+94177 p = 0 FAIL !!!!!!!!
BCFN(2+10,13-7,T) R=+56830 p = 0 FAIL !!!!!!!!
BCFN(2+11,13-8,T) R=+33230 p = 1e-8434 FAIL !!!!!!!!
BCFN(2+12,13-8,T) R=+16547 p = 2e-4200 FAIL !!!!!!!!
BCFN(2+13,13-9,T) R= +9358 p = 7e-2104 FAIL !!!!!!!!
BCFN(2+14,13-9,T) R= +4657 p = 1e-1047 FAIL !!!!!!!!
DC6-9x1Bytes-1 R=+522891 p = 0 FAIL !!!!!!!!
and the list continues...
Conclusion
All this to say that if you're going to do something inherently dirty, you might as well do it fast, and using sines isn't a good idea. You might as well multiply integers when possible (which isn't possible with JavaScript's representation of a number). Just "use a linear congruential generator" and pay your respects to John von Neumann.
Comments
No, like they said it is not possible to seed Math.random() but you can install external package which make provision for that. i used these package which can be install using these command
npm i random-seed
the example is gotten from the package documentation.
var seed = 'Hello World',
rand1 = require('random-seed').create(seed),
rand2 = require('random-seed').create(seed);
console.log(rand1(100), rand2(100));
follow the link for documentation https://www.npmjs.com/package/random-seed
Comments
Here's the adopted version of Jenkins hash, borrowed from here
export function createDeterministicRandom(): () => number {
let seed = 0x2F6E2B1;
return function() {
// Robert Jenkins’ 32 bit integer hash function
seed = ((seed + 0x7ED55D16) + (seed << 12)) & 0xFFFFFFFF;
seed = ((seed ^ 0xC761C23C) ^ (seed >>> 19)) & 0xFFFFFFFF;
seed = ((seed + 0x165667B1) + (seed << 5)) & 0xFFFFFFFF;
seed = ((seed + 0xD3A2646C) ^ (seed << 9)) & 0xFFFFFFFF;
seed = ((seed + 0xFD7046C5) + (seed << 3)) & 0xFFFFFFFF;
seed = ((seed ^ 0xB55A4F09) ^ (seed >>> 16)) & 0xFFFFFFFF;
return (seed & 0xFFFFFFF) / 0x10000000;
};
}
You can use it like this:
const deterministicRandom = createDeterministicRandom()
deterministicRandom()
// => 0.9872818551957607
deterministicRandom()
// => 0.34880331158638
Comments
SIN(id + seed) is a very interesting replacement for RANDOM functions that cannot be seeded like SQLite:
Comments
Most of the answers here produce biased results. So here's a tested function based on seedrandom library from github:
!function(f,a,c){var s,l=256,p="random",d=c.pow(l,6),g=c.pow(2,52),y=2*g,h=l-1;function n(n,t,r){function e(){for(var n=u.g(6),t=d,r=0;n<g;)n=(n+r)*l,t*=l,r=u.g(1);for(;y<=n;)n/=2,t/=2,r>>>=1;return(n+r)/t}var o=[],i=j(function n(t,r){var e,o=[],i=typeof t;if(r&&"object"==i)for(e in t)try{o.push(n(t[e],r-1))}catch(n){}return o.length?o:"string"==i?t:t+"\0"}((t=1==t?{entropy:!0}:t||{}).entropy?[n,S(a)]:null==n?function(){try{var n;return s&&(n=s.randomBytes)?n=n(l):(n=new Uint8Array(l),(f.crypto||f.msCrypto).getRandomValues(n)),S(n)}catch(n){var t=f.navigator,r=t&&t.plugins;return[+new Date,f,r,f.screen,S(a)]}}():n,3),o),u=new m(o);return e.int32=function(){return 0|u.g(4)},e.quick=function(){return u.g(4)/4294967296},e.double=e,j(S(u.S),a),(t.pass||r||function(n,t,r,e){return e&&(e.S&&v(e,u),n.state=function(){return v(u,{})}),r?(c[p]=n,t):n})(e,i,"global"in t?t.global:this==c,t.state)}function m(n){var t,r=n.length,u=this,e=0,o=u.i=u.j=0,i=u.S=[];for(r||(n=[r++]);e<l;)i[e]=e++;for(e=0;e<l;e++)i[e]=i[o=h&o+n[e%r]+(t=i[e])],i[o]=t;(u.g=function(n){for(var t,r=0,e=u.i,o=u.j,i=u.S;n--;)t=i[e=h&e+1],r=r*l+i[h&(i[e]=i[o=h&o+t])+(i[o]=t)];return u.i=e,u.j=o,r})(l)}function v(n,t){return t.i=n.i,t.j=n.j,t.S=n.S.slice(),t}function j(n,t){for(var r,e=n+"",o=0;o<e.length;)t[h&o]=h&(r^=19*t[h&o])+e.charCodeAt(o++);return S(t)}function S(n){return String.fromCharCode.apply(0,n)}if(j(c.random(),a),"object"==typeof module&&module.exports){module.exports=n;try{s=require("crypto")}catch(n){}}else"function"==typeof define&&define.amd?define(function(){return n}):c["seed"+p]=n}("undefined"!=typeof self?self:this,[],Math);
function randIntWithSeed(seed, max=1) {
/* returns a random number between [0,max] including zero and max
seed can be either string or integer */
return Math.round(new Math.seedrandom('seed' + seed)()) * max
}
test for true randomness of this code: https://es6console.com/kkjkgur2/
Comments
There are plenty of good answers here but I had a similar issue with the additional requirement that I would like portability between Java's random number generator and whatever I ended up using in JavaScript.
I found the java-random package
These two pieces of code had identical output assuming the seed is the same:
Java:
Random randomGenerator = new Random(seed);
int randomInt;
for (int i=0; i<10; i++) {
randomInt = randomGenerator.nextInt(50);
System.out.println(randomInt);
}
JavaScript:
let Random = require('java-random');
let rng = new Random(seed);
for (let i=0; i<10; i++) {
let val = rng.nextInt(50);
console.log(val);
}
Comments
You can use the seedrandom library
<script src="//cdnjs.cloudflare.com/ajax/libs/seedrandom/3.0.5/seedrandom.min.js">
</script>
It will add "seedrandom" to the JS global Math object, which you can use to get a seeded random number generator
var myrng = new Math.seedrandom('hello.');
console.log(myrng()); // Always 0.9282578795792454
Comments
Using seedrandom npm package in this example code:
const splitDataSet = (data, testSize = 0.2, seed = "anySeedValue") => {
// when you use { global: true }, the seedrandom package overwrites Math.random() with a seeded version, making all random numbers deterministic and reproducible.
const rng = seedrandom(seed, { global: true });
// data.slice creates shallow copy of data
// Since 0.5 - Math.random() is sometimes positive, sometimes negative, it randomly rearranges elements, mimicking a shuffle.
// const shuffled = data.slice().sort(() => 0.5 - Math.random());
const shuffled = data.slice().sort(() => 0.5 - rng);
const splitIndex = Math.floor(testSize * data.length);
const testData = shuffled.slice(0, splitIndex);
const trainData = shuffled.slice(splitIndex);
return { trainData, testData };
};
In machine learning, you want to train/test on the same split every time to compare models fairly. splitDataSet is used to split a dataset into training and test sets while ensuring that the split is deterministic using a seeded random number generator. This means that every time you run the function with the same seed, you will get the same shuffled dataset.
Comments
I have written a function that returns a seeded random number, it uses Math.sin to have a long random number and uses the seed to pick numbers from that.
Use :
seedRandom("k9]:2@", 15)
it will return your seeded number the first parameter is any string value ; your seed. the second parameter is how many digits will return.
function seedRandom(inputSeed, lengthOfNumber){
var output = "";
var seed = inputSeed.toString();
var newSeed = 0;
var characterArray = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','y','x','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','U','R','S','T','U','V','W','X','Y','Z','!','@','#','$','%','^','&','*','(',')',' ','[','{',']','}','|',';',':',"'",',','<','.','>','/','?','`','~','-','_','=','+'];
var longNum = "";
var counter = 0;
var accumulator = 0;
for(var i = 0; i < seed.length; i++){
var a = seed.length - (i+1);
for(var x = 0; x < characterArray.length; x++){
var tempX = x.toString();
var lastDigit = tempX.charAt(tempX.length-1);
var xOutput = parseInt(lastDigit);
addToSeed(characterArray[x], xOutput, a, i);
}
}
function addToSeed(character, value, a, i){
if(seed.charAt(i) === character){newSeed = newSeed + value * Math.pow(10, a)}
}
newSeed = newSeed.toString();
var copy = newSeed;
for(var i=0; i<lengthOfNumber*9; i++){
newSeed = newSeed + copy;
var x = Math.sin(20982+(i)) * 10000;
var y = Math.floor((x - Math.floor(x))*10);
longNum = longNum + y.toString()
}
for(var i=0; i<lengthOfNumber; i++){
output = output + longNum.charAt(accumulator);
counter++;
accumulator = accumulator + parseInt(newSeed.charAt(counter));
}
return(output)
}
1 Comment
In PHP, there is function srand(seed) which generate fixed random value for particular seed.
But, in JS, there is no such inbuilt function.
However, we can write simple and short function.
Step 1: Choose some Seed (Fix Number).
var seed = 100;
Number should be Positive Integer and greater than 1, further explanation in Step 2.
Step 2: Perform Math.sin() function on Seed, it will give sin value of that number. Store this value in variable x.
var x;
x = Math.sin(seed); // Will Return Fractional Value between -1 & 1 (ex. 0.4059..)
sin() method returns a Fractional value between -1 and 1.
And we don't need Negative value, therefore, in first step choose number greater than 1.
Step 3: Returned Value is a Fractional value between -1 and 1.
So mulitply this value with 10 for making it more than 1.
x = x * 10; // 10 for Single Digit Number
Step 4: Multiply the value with 10 for additional digits
x = x * 10; // Will Give value between 10 and 99 OR
x = x * 100; // Will Give value between 100 and 999
Multiply as per requirement of digits.
The result will be in decimal.
Step 5: Remove value after Decimal Point by Math's Round (Math.round()) Method.
x = Math.round(x); // This will give Integer Value.
Step 6: Turn Negative Values into Positive (if any) by Math.abs method
x = Math.abs(x); // Convert Negative Values into Positive(if any)
Explanation End.
Final Code
var seed = 111; // Any Number greater than 1
var digit = 10 // 1 => single digit, 10 => 2 Digits, 100 => 3 Digits and so. (Multiple of 10)
var x; // Initialize the Value to store the result
x = Math.sin(seed); // Perform Mathematical Sin Method on Seed.
x = x * 10; // Convert that number into integer
x = x * digit; // Number of Digits to be included
x = Math.round(x); // Remove Decimals
x = Math.abs(x); // Convert Negative Number into Positive
Clean and Optimized Functional Code
function random_seed(seed, digit = 1) {
var x = Math.abs(Math.round(Math.sin(seed++) * 10 * digit));
return x;
}
Then Call this function using
random_seed(any_number, number_of_digits)
any_number is must and should be greater than 1.
number_of_digits is optional parameter and if nothing passed, 1 Digit will return.
random_seed(555); // 1 Digit
random_seed(234, 1); // 1 Digit
random_seed(7895656, 1000); // 4 Digit
1 Comment
For a number between 0 and 100.
Number.parseInt(Math.floor(Math.random() * 100))
1 Comment
Math.random such that whenever Math.random is seeded with the same seed, it will produce the same successive series of random numbers. This question is not, per say, about the actual usage/demonstration of Math.random.