5

I'm using the following lines of code to populate the entries of l_new from l.

import random

l = [11.1, 22.2, 33.3, 11.1, 33.3, 33.3, 22.2, 55.5]

l_new = random.choices(l, k=30)
print(l_new)

I could generate a particular l_new always by fixing the seed using

random.seed(1)
l_new = random.choices(l, k=30)

Now, I would like to know if it is possible to find the seed for a given l_new when the seed is not fixed prior to the generation of l_new.

EDIT:

From what has been suggested below can I do

import sys
import random
l = [11.1, 22.2, 33.3, 11.1, 33.3, 33.3, 22.2, 55.5]
    
for i in range(10):
    seed = random.randrange(sys.maxsize)
    print("Seed:", seed)
    random.seed(seed)
    l_new = random.choices(l, k=30)
    print(l_new)

and save seed to retrieve the required l_new later on?

Natasha
  • 1,111
  • 5
  • 28
  • 66
  • Does this answer your question? [how to query seed used by random.random()?](https://stackoverflow.com/questions/5012560/how-to-query-seed-used-by-random-random) – Green Cloak Guy Oct 12 '20 at 03:23
  • @GreenCloakGuy Could you please have a look at my edit? – Natasha Oct 12 '20 at 06:29
  • Whatfor do you need the seed? It is just used to set the internal state of your random generation? See below for how to capture this state. – Patrick Artner Oct 12 '20 at 08:08

2 Answers2

3

It is not possible feasible to get "the seed" from a certain sequence of random numbers. See below how big the internal state of a mersenne twister ist.

Theoretically you could have two different seeds that produce the first 10 numbers identically to only then deviate - the more numbers you draw the less likely it is but the possibility is non-0 so no inverse possible.

If you do not set the seed it will use a seed created from the datetime:

random.seed(a=None, version=2)

Initialize the random number generator.

If a is omitted or None, the current system time is used [...]

seed() is just a way to set the internal start state of the mersenne twister that creates the random numbers.

However: you can get this state and store it and reset it:

import random

random.seed(42)

numbers = [random.randint(2,22) for _ in range(15)] 
print(numbers) # now you have another interal state 

d = random.getstate()
numbers = [random.randint(2,22) for _ in range(15)] 
print(numbers) # after drawing more numbers you now have another interal state 

# go back to the last state
random.setstate(d)
numbers = [random.randint(2,22) for _ in range(15)] 
print(numbers) # you now draw the same exact numbers 

Output:

# first numbers, using system time of 1st use as seed
[22, 5, 2, 10, 9, 9, 6, 5, 19, 4, 20, 15, 3, 2, 4]
# we remebered state and create another 15
[8, 9, 18, 21, 2, 19, 8, 22, 19, 15, 9, 16, 20, 10, 2]
# we reset state to last and create the same 15 again
[8, 9, 18, 21, 2, 19, 8, 22, 19, 15, 9, 16, 20, 10, 2]

Which kind of random method you use will influence the internal state changes.


Just in case you wondered how the state of random.seed(42) looks for me:

(3, (2147483648, 3564348608, 1266698288, 4212342371, 3595291661, 3180588708, 
     3037210256, 946923017, 2565409715, 2900535780, 924383152, 4180157270, 4230508198,
     2039675917, 3755350407, 2362848650, 2818100609, 2097423432, 524478045, 
     540883378, 281170210, 1485176884, 1493190386, 1773214509, 380915208, 3667698522, 
     2648371337, 2961234806, 3857480267, 1582950522, 246289694, 3322185604, 
     1944574775, 302623699, 169865066, 1143540808, 3733177770, 513116636, 1411153081,
     3205493053, 768926902, 549624109, 1470655403, 59539609, 3678480009, 3087139671, 
     1176835859, 2078491503, 2299934332, 1592059249, 1062716176, 2654193596, 
     3531838733, 2661260596, 3881209635, 2106865768, 4154287292, 2082185616, 
     2301197011, 2177349827, 3082181756, 1787663536, 3714670796, 3018262113, 
     1670056238, 1856738750, 99824592, 2279837081, 1414647942, 3416675731, 3458782472, 
     3997022236, 468762002, 2666158583, 953353270, 1788980658, 3802061067, 407586584, 
     1844776834, 1906917274, 3154715663, 3028370222, 4156024188, 3996363428, 80495456,
     2659800972, 2005649973, 3818358673, 3952623596, 2506862371, 3282302532, 263923435,
     3384662671, 3292439172, 3119957588, 1224426111, 899864150, 215262826, 1619647231,
     3347694949, 3497868538, 2029552053, 2992804824, 4080010250, 2023513186, 1885979437, 
     3564622190, 3775424270, 2297810139, 3549449169, 2664856277, 3274801974, 2794883969,
     980412666, 2980215653, 2794389321, 2816521934, 1266970739, 542306338, 3646225311, 
     3598997630, 2111980720, 2949252482, 2489027658, 352815024, 11610683, 1386663624,
     2004196796, 1161461546, 1921293780, 2463949525, 1647009713, 3550093655, 2563894064,
     3486310554, 1506105865, 243092931, 2659437476, 4200687059, 2284345122, 1974438610,
     3591096528, 967119212, 3362401375, 140678365, 311602112, 2361740275, 2139598582,
     3632873481, 2762232439, 4156482318, 381637792, 3253346525, 2492118775, 1502434558,
     3164497290, 3550998357, 2412448305, 2223955385, 4122879535, 350121793, 1835149778,
     2175117867, 989674750, 3178241202, 3553093569, 3470650311, 2829698151, 3209427769,
     1779174943, 275388428, 4044574515, 715447260, 3180940440, 4020772289, 1322708567, 
     3189868792, 4250485633, 716970023, 2307550151, 1074996711, 1217573599, 197006094,
     2178394212, 1255233746, 4164251484, 1405608772, 2808160475, 1304736088, 1796071066,
     2761748078, 3570739698, 1616118556, 2232868135, 3567541936, 3470600401, 3031621994,
     3351764214, 1359785149, 2617497797, 3340028190, 356162828, 2083806068, 2503635608, 
     4024838996, 2577080371, 2897993505, 3120733934, 905794891, 2506078507, 4211618666, 
     3777871979, 809751414, 4080874167, 1562977008, 3917373055, 2132779194, 4014249473, 
     4067327082, 2582869847, 1780081876, 1842619106, 3381761227, 921004274, 1393256920,
     1883566732, 2702071861, 865327389, 1622085203, 3021825820, 2687061406, 1748902923,
     689023977, 308399650, 2377287978, 1646969411, 1051806316, 4277884230, 2041056290, 
     101134519, 2032472116, 4112521069, 151202901, 2773743461, 551348559, 3476836808,
     510935951, 625057077, 3757450756, 2977698135, 3027776859, 2616998041, 2773430005, 
     544190486, 2241368212, 1141105829, 1452816309, 4199229235, 3218013033, 4229475816, 
     1659576351, 3020348754, 1193400518, 3208584597, 1151197733, 2597187966, 503065140,
     2421841572, 1437291709, 1909275895, 2872630545, 793588217, 3792934707, 1784451785, 
     2921385648, 1669902526, 4189978976, 1196986251, 434805516, 1907541826, 2624415034, 
     1687778718, 650746582, 1949153382, 4148493093, 841300520, 1164202054, 4203468658, 
     4106300911, 850346789, 1715730760, 3114661489, 2866524548, 1360448945, 3601318775,
     1743078223, 2413855408, 1211895622, 325117146, 2721152875, 1284334485, 2446538832, 
     739014618, 2237045115, 842553465, 2538598293, 746460793, 4010387366, 2002655192, 
     4193733112, 1194380773, 3918217378, 1447487475, 5659228, 3408847694, 4190318700, 
     1862549564, 781683719, 1194618118, 755053413, 3436011942, 2885435303, 3081151348,
     2017642831, 1053816502, 1086627485, 2157296554, 110650022, 965352898, 1003174194,
     1288956241, 4057404871, 2965068465, 2897064481, 2457377317, 1879872545, 358455290, 
     375086701, 3015902095, 1676249984, 924455526, 2084169389, 1989014644, 1993749926, 
     2009424973, 2113340508, 3980883273, 2915977458, 203328382, 3020815229, 2415050113,
     4103009585, 3700885489, 2916647550, 1523006503, 174302338, 2476909338, 1969322490,
     4285741984, 1528449097, 3355315515, 4217241278, 599579127, 2572243673, 3035856735, 
     1539140489, 1782314913, 4238644287, 1746424142, 1978148312, 2380746849, 184941882, 
     1106717981, 1720750349, 981701307, 3953154731, 3257809181, 2892339376, 3339778166, 
     3676936849, 87425948, 3029257381, 2037942523, 3807628706, 2861474706, 1058852346, 
     1322765211, 2686046342, 2689342655, 2303436168, 2571627181, 1986057734, 1183564308,
     2829677523, 1295563975, 503126586, 2025890348, 4179277821, 1735262467, 981331774, 
     1613447066, 1011606109, 2000062246, 3581448390, 3477731384, 3641307373, 3508544379,
     2327233491, 3931944343, 4189052882, 2990416380, 422406169, 202291313, 2531006461, 
     4277024116, 3815144003, 821314585, 1344175168, 3562834071, 1339615445, 1831545190,
     3115548822, 743512780, 4006999448, 3720181735, 1012033521, 919931041, 2628967879, 
     1151876565, 1268107129, 3674829936, 834977846, 743987006, 3947536548, 3706529695, 
     4121073678, 2507605742, 1595636918, 2708047833, 2427507331, 3868216331, 3254240010,
     2097683411, 3279710596, 3686819053, 1843541720, 1683793619, 3245287285, 3571828776, 
     3733296431, 3806747478, 1390930605, 3860422228, 114397037, 1931519825, 2770684378, 
     1556101783, 1436111731, 4031950081, 562876656, 1775895782, 612364620, 1313509772, 
     4283410242, 3252958463, 2176555836, 3933073367, 3013277102, 1444071961, 3120949516, 
     2824578890, 325676929, 943677134, 1800649256, 1721927060, 347498719, 1435221321, 
     2623572981, 1408548470, 4145586315, 2901889237, 1849377952, 1239144551, 3382598266,
     2992893897, 3738297588, 611280106, 3897415338, 2370299241, 1772308583, 3697465753, 
     354508058, 2702360134, 591308331, 3524072501, 976616000, 2563717192, 3078266097, 
     1376594703, 4209795919, 2454412767, 2712206031, 2963860163, 3734324882, 2248653800, 
     324872786, 3789837448, 3779000146, 527733939, 2844165793, 576499681, 1618787435, 
     2638888650, 57511068, 2804627518, 2993670030, 481402236, 2810124845, 1416045214, 
     1723694191, 1214944572, 3188123783, 1139185907, 3851015362, 1719652470, 1661343029, 
     3644307578, 3564178709, 1256656955, 46631590, 4231317929, 3098958589, 1834956625, 
     2206185428, 3695688374, 3647957317, 1064098871, 1739100906, 2579568980, 27974051,
     2617466775, 964075233, 907049942, 4164146575, 3377168066, 2524828266, 1083546008,
     2992960953, 2260789066, 1543742095, 2843842831, 1375722284, 3574521313, 110842534, 
     2310998251, 3076511734, 783145600, 1287776608, 3087144146, 305559823, 2356293719, 
     3228441476, 1678938122, 3775814061, 1620283952, 2512027726, 1031432407, 962295099, 
     3877418501, 968669928, 304126693, 3711291137, 3847527101, 494066767, 4050229756, 
     4169448589, 671763915, 1095747781, 4006132710, 394725957, 200521654, 2715998750, 
     1477567673, 895171901, 3370105999, 2684157455, 4153990023, 3966076501, 2043374409, 
     144443759, 6764556, 1611650045, 1480956755, 1388276468, 4136518438, 1538041336, 
     266773992, 1623357516, 2267298390, 3183919402, 1084292424, 2796136160, 2413448816,
     2850375199, 3510894040, 2644778623, 3317288284, 3697317540, 1465776787, 1843489446,
     1416711171, 744701117, 1286781349, 3748640476, 861982119, 2377742909, 1171768136, 
     2701877439, 3839724288, 2869791015, 2386067954, 2629214347, 955801623, 3831079317,
     624),
 None)
  
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • 2
    It usually *is* possible to determine the RNG state from a sufficiently long sequence of random numbers. There are only finitely many possible states, and for a long enough output, there's typically only one possible state that would have produced that output. "Long enough" may be pretty long, though, especially for the Mersenne Twister, which uses a gigantic state, far beyond the point of providing any practical benefit. – user2357112 Oct 12 '20 at 08:18
  • @user2357112supportsMonica What do you mean by "gigantic"? The internal state is only 625 numbers and the same number of outputs is sufficient to restore the internal state (see [for example this](https://github.com/eboda/mersenne-twister-recover)). – a_guest Oct 12 '20 at 08:45
  • Thanks a lot for the answer, could you please clarify if `random.setstate(d)` will generate `[8, 9, 18, 21, 2, 19, 8, 22, 19, 15, 9, 16, 20, 10, 2]` whenever the code is run or would it vary as a function of `sys` time? – Natasha Oct 12 '20 at 08:46
  • @Natasha the state you gather from the random defines its internal state. If you draw any kind of random from it this state changes. As long as you use the same random operations the state changes in the same way so you create the same order of random numbers – Patrick Artner Oct 12 '20 at 08:52
  • @a_guest: That *is* gigantic. Even cryptographic RNGs don't use anywhere near that much state. Manipulating all that state is a waste of cache, and the statistical properties of the Mersenne Twister aren't even better than competitors with states two orders of magnitude smaller. – user2357112 Oct 12 '20 at 09:06
0

This is not possible due to the fact that the random output of the RNG gets mapped via floor and that relationship is not an injection:

return [population[floor(random() * n)] for i in _repeat(None, k)]

So even if you knew which elements got selected from the original list l, you cannot tell what output random produced and hence you cannot deduce the internal state of the RNG.

a_guest
  • 34,165
  • 12
  • 64
  • 118