Advise how to make a pseudo-random generator, initialized by a number, with which you can generate a sequence of numbers (several thousand numbers from 0 to 9). The main task is that when you start the same generator with the same number of initialization, it gives the same values.

  • Also interesting, the right thing - Sh4dow
  • one
    Brrr .. Numbers Numbers Numbers consist .. of numbers! - LinuxFan

3 answers 3

The simplest option:

var num; // очень большое простое число. var seed; // затравка - то самое инициализирующее число. var start_number; // основа var counts = new Array(); var max_num = 10; // Инт, который не превосходит наше число. У вас 0-9 числа. for(var i=0; i<1000; i++){ counts[i] = (Math.pow(start_number+i,seed)%num)%max_num; } 

The mechanism is simple: we build a foundation that is changed by the counter to a power of a seed and take the remainder of the division by a large number.
Then we take the last few numbers (in our case, one).

How to optimize: binary exponentiation (using the fact that we have a ring)
Remarks:
1) num should be large enough to provide diversity.
2) the number in the seed degree must, however, exceed this prime number.
3) the foundation can be abandoned altogether if the incrementor's account starts with 2, and the seed is taken, at least, from ~ 20

  • And if without degrees and roots? Perhaps only with addition and logical operations? - sergiosas
  • If you still want to realize yourself, the degree is easily synthesized from additions, and the search for the remainder is from subtractions. = B A simpler method, I'm afraid, will entail a much more predictable and less uniform sequence. - knes
  • Your option is good - 20,000 numbers are generated without repetition and delay! Thank! - sergiosas

And it doesn’t bother people to invent not only bicycles, but even bast shoes ...

Linear congruential method .

    The issue was successfully discussed on the original SO: https://stackoverflow.com/questions/424292/how-to-create-my-own-javascript-random-number-generator-that-i-can-also-set-the-s