I would like to choose a random seed for numpy.random
and save it to a variable. I can set the seed using numpy.random.seed(seed=None
) but how do you
If you want you can also save it in a json file and then unpack it and then use it again. Since numpy stuff can't be serialized you need to serialize it yourself but its not that bad:
One file:
import json
import numpy as np
def put_numpy_seed_in_json_dic(results):
(rnd0,rnd1,rnd2,rnd3,rnd4) = np.random.get_state()
rnd1 = [int(number) for number in rnd1]
rand_seed = (rnd0,rnd1,rnd2,rnd3,rnd4)
results['rand_seed'] = rand_seed
return results
def get_numpy_seed(results):
(rnd0,rnd1,rnd2,rnd3,rnd4) = results['rand_seed']
rnd1 = [np.uint32(number) for number in rnd1]
rand_seed = (rnd0,rnd1,rnd2,rnd3,rnd4)
return rand_seed
then run it to save the seed:
import json
import numpy as np
import my_rand_lib as mr
results = {'rand_seed':None}
results = mr.put_numpy_seed_in_json_dic(results)
print np.random.rand(1)
print np.random.rand(1)
print np.random.rand(1)
fpath = './rand_seed_file'
with open(fpath,'w+') as f:
json.dump(results,f)
print '... doing other stuff'
with open(fpath,'r+') as f:
results2 = json.load(f)
print 'other ',np.random.rand(1)
print 'other ',np.random.rand(1)
print 'other ',np.random.rand(1)
print '... done doing stuff'
rand_seed = mr.get_numpy_seed(results2)
np.random.set_state(rand_seed)
print np.random.rand(1)
print np.random.rand(1)
print np.random.rand(1)
and if you don't want to generate a seed everytime you run it you can have:
import json
import numpy as np
import my_rand_lib as mr
fpath = './rand_seed_file'
with open(fpath,'r+') as f:
results2 = json.load(f)
rand_seed = mr.get_numpy_seed(results2)
np.random.set_state(rand_seed)
print np.random.rand(1)
print np.random.rand(1)
print np.random.rand(1)
I tried this on a remote server and I always get matching random numbers:
[ 0.90741273]
[ 0.6861296]
[ 0.21714398]
not sure if this is interesting but this was the seed (which is a tuple):
{"rand_seed": ["MT19937", [3244492226, 4276548057, 571402114, 3235873143, 4078239958, 1440625038, 4042777784, 3400010150, 1164584760, 271139028, 1264217608, 1403324904, 234696259, 623484078, 3424719234, 3896351743, 1818071683, 3077380191, 2989066157, 3828180331, 2032001745, 1137603205, 1993713826, 873523654, 3267461254, 2964954176, 3217679339, 4079232021, 1182272168, 402998421, 968119626, 2151162455, 2550226639, 3522780791, 245256811, 2866158388, 587411937, 2836234133, 3485394274, 1767143488, 3772379711, 1244725495, 1061026769, 2544419920, 3963050848, 232749713, 2084368489, 1990090546, 2883903063, 174001222, 2569537698, 517341511, 2366955295, 1830324490, 2388090514, 1637855850, 1383101875, 2719629528, 885528387, 7941101, 2769663894, 2704541593, 3129289945, 2681434614, 3308402481, 2161196492, 2896442132, 1474561199, 156414990, 2934014108, 2740454316, 4029663532, 2903418479, 118978587, 3095335574, 1044532364, 2629619463, 623783821, 3172307947, 2539001597, 2020636966, 404303542, 373288588, 289388097, 1050356390, 1126919064, 474676333, 2156863001, 92975776, 1204572119, 1341956590, 4284155262, 3380981209, 1268302262, 835613316, 623125230, 1150083001, 3444902937, 2318349536, 2881496834, 393068269, 28626933, 2931354423, 2014174400, 4212996966, 3105086458, 74404022, 413795342, 3782258177, 3626466932, 1932129332, 3538419256, 943472124, 963175815, 4076955699, 52410025, 318657184, 839799912, 2150435130, 3187525421, 2124551508, 3930704180, 2375548757, 497820208, 422355274, 260159836, 3437157934, 1301403840, 4057357702, 3217300631, 2910194797, 1972036860, 624838554, 3418367281, 3823714808, 1342594222, 3874939587, 3578421466, 3997730187, 751930224, 801189513, 1225089722, 910752086, 1415351761, 4287089458, 224210780, 643596696, 1030838729, 1924676141, 2579935013, 32904138, 2486616018, 1665731347, 642496995, 577928776, 4119274366, 1438990597, 885648199, 2401966414, 1937630298, 2029522084, 3823943785, 1652388617, 242507028, 163957584, 197993457, 3003700508, 2357598705, 479742798, 2159530434, 2641855048, 1153321528, 458640940, 1364908158, 3931878737, 3754891907, 733317650, 3631844997, 209681576, 780025499, 217109730, 2659949782, 164210317, 2234081627, 2798187303, 3793035212, 622613442, 4027945659, 1264924240, 3755962138, 168637328, 4193297896, 593711399, 2018193001, 696136156, 3343926759, 3938753383, 3549915312, 2049590636, 1732826453, 3770804132, 1544263650, 3623494103, 1454784121, 860580298, 1336846278, 3298403325, 4156569419, 51196786, 3398541940, 717201402, 1418590160, 3407195989, 293192063, 3871127471, 963318294, 3177164855, 2248856336, 2363561954, 2122436074, 3083439454, 331898151, 3489466823, 1480231253, 3727404028, 1942269624, 3342915239, 2451833278, 1279324699, 3678779848, 494256563, 170826038, 3200966622, 3284372389, 3798475074, 191206256, 1112201427, 3959301392, 43618741, 1358008929, 2972254642, 2250013335, 659600256, 720199815, 1355589829, 1511937267, 2090180739, 2779086170, 704140912, 1354505400, 4106508219, 4130987887, 1135113560, 3310205054, 2559493616, 3994237157, 2449530906, 1017478859, 2475414025, 260408932, 3882314025, 3169908095, 1431718224, 755730563, 4129813635, 482751982, 42657908, 2418940148, 2380660631, 3596648617, 2668040386, 3700947086, 1235361153, 4212839143, 2803192914, 679783840, 1396721631, 3549531060, 3714188947, 1582886984, 3930587164, 1787845200, 1878170563, 3998685888, 275016726, 1362149445, 1784854500, 3413367687, 999979145, 30464988, 1781846287, 2052179802, 614372595, 1795389478, 3837746383, 1716252322, 1496633789, 1913960414, 3824749341, 745150948, 2990885936, 3557188824, 1853716952, 226442384, 3881419361, 3877508921, 2125849259, 3725330620, 4249819850, 1866002740, 3954375926, 1263697298, 2359110923, 3704149399, 3915156522, 2720534920, 2240262865, 1298116022, 2430494738, 3106481019, 1118448263, 3386525375, 3850025930, 947096317, 2014058358, 2943385566, 1639655978, 824538918, 2893393554, 190010755, 918084027, 4197568458, 2308675470, 3969533604, 823650146, 3971685975, 3959021418, 2335451148, 3651109937, 3536101054, 2028026981, 1042621858, 2093418547, 3332527479, 345797902, 1962843497, 1651609280, 849683942, 701440541, 3001603849, 2547855201, 2847179356, 2686463194, 2556105058, 2957249371, 4122354156, 4095666057, 3269707747, 2075948426, 4189148196, 59188700, 1425136277, 4010662242, 403095998, 2435933607, 3254626634, 320429604, 921618676, 4179054005, 1590495757, 362965764, 3892792894, 4264771139, 300303781, 4194594842, 1773582295, 1792749320, 3114744569, 3059831369, 543108826, 605116437, 1206221920, 3763708911, 3474933214, 933590768, 4096747554, 2732890014, 1180321103, 3174872523, 2361419553, 303084740, 3438967187, 829657141, 3976738932, 3250508727, 2965752967, 2766618501, 2610047728, 3913791738, 2383381107, 2911412379, 2570048205, 1059652767, 1105153800, 258287599, 1366361775, 1043101709, 4136777479, 4002476750, 2242511114, 1937386895, 2318776696, 3919577988, 819932046, 2154232126, 2359937340, 1707529303, 1430709021, 57940224, 2463543918, 439698027, 2154236676, 2989369870, 2711983380, 1243586438, 1648109179, 234677646, 1369164631, 3246772730, 1150951970, 707111532, 2641066313, 1561023105, 2352529521, 3905609297, 3758075920, 4124559541, 3768803924, 3443976002, 2619333832, 3399759018, 2295667887, 4126858561, 3139541980, 2382271429, 4033423715, 648775734, 2777131955, 1929238235, 2146942632, 1115329972, 3985641642, 2007135435, 3551753547, 2967740448, 4196112540, 61581572, 886344810, 4097187928, 1166916633, 3890455280, 1473584306, 1440678763, 2848991175, 2493980496, 3967544385, 1757152663, 52315252, 2476642029, 2727074449, 4197000746, 2878883929, 144032869, 3517610268, 3758074755, 164078969, 4288210033, 1130401207, 2376285572, 3726677017, 2021546352, 2763363362, 791950895, 1834778577, 3067448324, 2618082688, 4194263605, 84230440, 624358904, 3203686228, 2014115933, 1844566018, 314698511, 1096366940, 1413533306, 2490690918, 3524310116, 3912232452, 3595400103, 2104097721, 2277699865, 2127808758, 890104002, 4261780514, 3943759279, 2421596910, 3462302371, 3114202694, 3301664792, 3958641805, 3828288008, 3138631754, 2707054121, 873889048, 360096040, 1277036249, 310404450, 1841086653, 1324064291, 1069123460, 3667889879, 3549162319, 4105010862, 3802778145, 1818048305, 3083126999, 3810922140, 2364932315, 2667079274, 3034477663, 1142598277, 2129656233, 2900596493, 1771721766, 2091125900, 2024931777, 2186939139, 4292757779, 3168005700, 2700706967, 2033965363, 2815886839, 1936909550, 1018210446, 2494829103, 3182190430, 4070030839, 3878343946, 3290625485, 2885062721, 3427598831, 3748858811, 2021454997, 2926497731, 3462334646, 747641905, 2870980834, 1072943394, 370913272, 519334913, 3099507262, 698616436, 3884871568, 2530196197, 223690634, 1816574877, 2872502342, 3629966511, 4040316403, 400367036, 1898479168, 1795033191, 4090946019, 938326326, 1509105095, 886381170, 4207241822, 2919702734, 1437184594, 2765872952, 561764883, 3441440757, 1219765705, 209412518, 1098738818, 3782425126, 3113624586, 3302772981, 1213966890, 4292826280, 4109015079, 1949958581, 320991923, 1070765942, 2002780881, 3364869673, 3039286974, 1824574474, 1266616388, 703321141, 2004303453, 1284326590, 728587648, 427042526, 2160662521, 2783764788, 3053336315, 3542331332, 2881174731, 4160514263, 3326878203, 4139791808, 2639767143, 3144886711, 480269073, 2318151636, 3594165209, 1629301762, 1786754501, 1157007028, 1415023980, 172137771, 3444342355, 3889095376], 624, 0, 0.0]}