There is a random number generator in the language that is not tied to any parameter, but the opposite should be done.
Above the complicated is not necessary, but absolutely simple would also not be desirable.
There is a random number generator in the language that is not tied to any parameter, but the opposite should be done.
Above the complicated is not necessary, but absolutely simple would also not be desirable.
Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .
The classical linear congruential method can be an acceptable solution if there are no cryptographic strength requirements.
Taking into account the specifics of JavaScript, it is easier to take the generator parameters used in the standard minstd_rand
function from c++11
:
multiplier = 48271 increment = 0 modulus = 2147483647
In the case of multiplying the multiplier
(48271) by the maximum seed
value (2147483646), we get 103661183076066, which is less than the maximum safe whole number in JavaScript 9007199254740991 .
var Random = function(seed) { seed = (seed || 1) % 2147483647; return { next: function() { return seed = seed * 48271 % 2147483647; }, seed: function(s) { seed = s; } }; }; var random = new Random(1); for (var i = 0; i < 1000; i = i + 1) { console.log(random.next()); }
To verify the results, you can use the code in c++11
:
#include <iostream> #include <random> int main() { std::minstd_rand g(1); for (int i = 0; i < 1000; ++i) std::cout << g() << std::endl; }
Link to valid C ++ code. Both variants give the same result at the end of a sequence of 1000 iterations:
... 390639274, 1641974594, 429183498
How to limit the result by range, I think you know.
Mersenne Twister or any linear congruent use. You can still take from Knut or from glibc
In general, I decided to answer my question, maybe someone would comment, maybe I did something wrong.
I use the example from the wiki article Linear congruential method
The only thing is: I eliminated the clipping of digits (sort of) and instead of the integer I return a fractional number from 0 to 1.0.
var Random = function(seed) { var A = 1103515245, C = 12345, M = 0x80000000; seed = seed || 1; return { rnd: function() { seed = (A * seed + C) % M; return seed / M; }, seed: function(s) { seed = s; } }; }; var seedInp = document.querySelector('#seed'); var out = document.querySelector('#out'); document .querySelector('#rndBtn') .addEventListener( 'click', function() { var numbers = []; var random = new Random(seedInp.value); for (var i = 0; i < 200; i++) { numbers.push(random.rnd()); } out.innerHTML = numbers.join('\n'); } );
body { font-family: sans-serif; }
<label>Seed: <input type="number" id="seed" value="1" /> </label> <input type="button" value="Rnd" id="rndBtn" /> <pre id="out"></pre>
seed = seed * A + C; return (unsigned int)(seed / 65536) % RAND_MAX;
seed = seed * A + C; return (unsigned int)(seed / 65536) % RAND_MAX;
I have a seed = seed * A + C; return (seed % RAND_MAX) / RAND_MAX;
seed = seed * A + C; return (seed % RAND_MAX) / RAND_MAX;
- Aleksander K.Source: https://ru.stackoverflow.com/questions/554045/
All Articles