Hello! There is a set of numbers from 0 to 15 inclusive, there are about 70. We need a PRNG that can generate all of these numbers using some kind of seed. In general, an easily reversible PRNG is needed for this task, so that it can also easily calculate the seed for which these numbers were generated. What do you advise? I am writing to php. The numbers themselves were generated on perl.
- oneThere are a couple of options on stackoverflow.com/questions/2911432/… - Arnial
- I will advise again: do not write in PHP, because the speed. And second: study the PRNG that was allegedly used. - Vladimir Martyanov
2 answers
PHP has a built-in random number generator: rand ()
Read / learn what suits your task best.
About the seed:
You either set it yourself initially and get a reproducible situation or the system generates it at the time of the request.
If you need to find a seed by numbers, this is not a trivial mathematical task:
Here in this article you can find the formulation of such a problem (section Linear congruent PRNG (LCPRNG))
if you really need to - you can write code to calculate the seed (solving this problem, respectively)
- We really need a solution to the problem! I would be very grateful if someone can help solve. - user262975
- Here is a set of numbers: 6, 8, 5, 2, 9, 10, 10, 7, 13, 1, 15, 3, 5, 3, 10, 8, 9, 9, 8, 9, 14, 2, 13, 10, 14, 0, 15, 1, 13, 10, 9, 12, 3, 13, 1, 13, 9, 0, 11, 12, 3, 12, 14, 5, 12, 14, 15, 10, 10, 3, 2, 6, 5, 14, 8, 1, 15, 4, 0, 6, 0, 12, 0, 8. Help write the code to generate such numbers through the PRNG and calculate the seed itself, which was used for generating numbers There are also similar sets of numbers, the length is the same, you need to calculate a seed from them, with which you can generate these sets of numbers. - user262975
- In the sense of generating exactly such and in that order? Where do the numbers come from? Are you sure that these are numbers from the standard random? - Dejsving
- Just if the numbers with / dev / urandom for example - in them may not be patterns. Right now I am not ready to offer you any code. At the weekend, if you do not burn think. - Dejsving
- Restoring a sid for a standard rand () is a non-trivial task mathematically, but trivial from a programming point of view: no more than 2 ^ 32 seats and a complete search takes about a couple of hours. - Vladimir Martyanov
Restoring the "generator" by recruiting its samples is reduced to restoring three parameters: the generation algorithm, the internal state of the generator, and the algorithm for converting raw generator readings into the available data.
There are many generators: Type3 generator, msvcrt generator, Mersenne Twister, WinAPI, OpenSSL and so on. The first three can definitely be used in PHP. The operating system, build options, and so on can affect the generation algorithm.
Internal state depends on the type of generator. MSVCRT, Type3 use a 32-bit integer for the state, in fact, seed. Mersenne Twister has a much larger internal state, which changes upon receipt of each sample. WinAPI definitely can add new data to the state, OpenSSL also seems to be.
In the simplest case (Type3, msvcrt, Mersenne Twister), it outputs a 32-bit integer as a result. Further conversions must be carried out with it to obtain the desired range of values. Your data is similar to data[i] = random_raw() & 0x0F or, which is the same in this case, data[i] = random_raw() % 16
The general method is a hypothesis about the type of the generator, about the conversion and then it is checked for each sid. If the triple generator-sid-conversion gives the same sequence - we can say that this triple could (but should not have !!!) be used for generation.