gpt4 book ai didi

python - 如何找到随机数生成器的种子?

转载 作者:行者123 更新时间:2023-12-04 08:46:05 30 4
gpt4 key购买 nike

我正在使用以下代码行从 l 填充 l_new 的条目。

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)

我总是可以通过修复种子来生成一个特定的l_new

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

现在,我想知道当 生成之前种子未固定时,是否有可能找到给定 l_newseed l_new.

编辑:

根据下面的建议,我可以做什么

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?

最佳答案

不可能 从某个随机数序列中得到“种子”是不可行的。请参阅下面的梅森扭曲器的内部状态有多大。

理论上,您可以有两个不同的种子,它们产生的前 10 个数字完全相同,然后才出现偏差 - 您绘制的数字越多,出现这种情况的可能性就越小,但可能性不为 0,因此不可能出现倒数。

如果您不设置种子,它将使用从日期时间创建的种子:

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

Initialize the random number generator.

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

seed() 只是一种设置创建随机数的 mersenne twister 的内部启动状态的方法。

但是:您可以获取此状态并将其存储并重置:

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

输出:

# 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]

你使用哪种随机方法会影响内部状态的变化。


以防万一你想知道 random.seed(42) 的状态如何寻找我:

(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)

关于python - 如何找到随机数生成器的种子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64311236/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com