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.

Closed due to the fact that the essence of the issue is incomprehensible to the participants dirkgntly , user194374, lexxl , jfs , D-side 11 Aug '16 at 14:42 .

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 .

  • language - javascript? - Vladimir Gamalyan
  • @VladimirGamalian some PHP variants behave in the same way ... - Vladimir Martyanov
  • 3
    The answer is any PRNG . Different algorithms may have different properties. What do you want? Despite the seeming simplicity, you should not try to implement them yourself, if this is not an exercise for educational purposes. - jfs
  • Yes, the language is js. Regarding the properties - I do not pursue goals related to cryptography, rather, just the generation of the world / names, etc. - Aleksander K.
  • In general, I left the answer here, rewrote the linear congruential method. If you can see whether it is true. - Aleksander K.

3 answers 3

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

    • Unfortunately, this is the answer-link, not the answer - Kromster
    • @Kromster you, right, propose to write the implementation of a dozen GPSNG suitable for the author, and in some language that is not clear? - Vladimir Martyanov
    • I would suggest to tell a relatively simple algorithm and give a link to the literature as a supplement, and not to be limited to just one link. - Kromster

    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> 

    • one
      From the original algorithm from Wikipedia far gone .. - Vladimir Gamalyan
    • Why? In the original: 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.
    • Only now he begins to give NaN after some iteration ... well, the seed goes to Infinity ( - Aleksander K.
    • one
      In the original, 32-bit integer arithmetic, so your infinity leaves. - Vladimir Gamalyan
    • one
      Well, if not to assume that this is already a self-made generator, then everything is OK) - Vladimir Gamalyan