Find a seed that creates a specific number sequence with random.choice(range(128))?

Viewed 488

I want to find a seed that creates a specific number sequence:

[115,91,45,76,78,93,35,5,29,8,99,88,98,70,40,116,11,39,102,41,124,98,120,57,36,67,57,23,52,34,75,32,117,66,12,19,86,67,62,121,60,5,54,37,65,18,5,56,66,115,32,99,73,70,115,73,123,74,31]

I wonder if I could find one of seed that give me this result with the function get() I created :

def get():
   seed(x)
   return [choice(range(128)) for _ in range(59)]

with x a constant equal to the number that, apply as seed, give me the right top above sequence.

This is a little program I made to expect find it, but right now I'm about 1.6 milions tested seed and still nothing.

from random import choice, seed

lc =[115,91,45,76,78,93,35,5,29,8,99,88,98,70,40,116,11,39,102,41,124,98,120,57,36,67,57,23,52,34,75,32,117,66,12,19,86,67,62,121,60,5,54,37,65,18,5,56,66,115,32,99,73,70,115,73,123,74,31]

sd, h = 0,0
while 1:
  seed(sd)
  for c, o in enumerate(lc):
    if not choice(range(128)) == o:
      if c > h :
        print(f"[Seeed {sd}] {c} matchs")
        h = c
      sd += 1
      break

Can someone help me to find one of the right seed ?

3 Answers

Using brute force and assuming that every seed represent an extraction from your set of number (128), with replacement, you have a probability of

1/(128)^59 = 1 / 2.1153791001287955166461289857048673274508949854856999 × 10^124

for every extraction to get your exact set of numbers (assuming uniform distribution for every number extraction of your random function). Which is a probability pretty near to zero.

So yes. You could hang (almost) forever for that brute force search

I hope it is not possible. Technically, it is possible to code a quasi-random generator that allows restoring a seed by a short sequence of results. But normal quasi-random generator should disallow that. E.g. for quite common Mersenne Twister the internal state is 624 ints. But your seed is just one int. Even if you brute-force the seed that gives you’re the same short sequence, the whole internal state actual may be different and consequent generation will goes completely other way.

Any seeded PRNG will have a formula to generate the next number from the internal data it holds. With something as simple as a Linear Congruential PRNG then it is easy to back-calculate the internal data and the numbers used in the formula from the output. With a more complex PRNG, such as Mersenne Twister, then the back-calculation becomes very difficult.

One solution would be to copy the sequence of numbers you want and store them somewhere, pulling them from the store as needed. Alternatively read the documentation of the PRNG used to generate those numbers initially to see if a back-calculation is possible.

If the numbers came from a cryptographically secure PRNG then your task becomes orders of magnitude more difficult.

Related