前言:
此刻朋友们对“python27升级”大概比较关切,小伙伴们都需要分析一些“python27升级”的相关资讯。那么小编在网上汇集了一些关于“python27升级””的相关内容,希望我们能喜欢,咱们快快来学习一下吧!Python 随机数模块
这一篇文章继续介绍Python的内置模块,有时候我们回经常要生成一些随机数,由于测试,记录,比如说生成一串唯一标示。所有python的随机数在现实的应用开发中会经常用到,常常作为辅助的角色。这一篇文章将介绍Python的随机数模块random。random模块中的随机数是伪随机数。
常用函数
random.seed()
设置随机种子 ,用于同步不同运行环境的随机数,默认不赋值的话返回None。
random.seed(a=None, version=2)
random.getstate()
获得当前状态,用于恢复状态
random.setstate()
恢复状态
random.random()
随机产生一个[0,1.)数字。
random.uniform()
random.uniform(a, b)
产生一个a、b区间的随机数。
random.randrange(start, stop[, step])
整数随机。
random.randint(start, stop)
返回一个[start,stop]的随机整数。
random.choice(seq)
返回对象中的一个随机元素。
random.choices(population, weights=None, *, cum_weights=None, k=1)
随机选择,是random.choice(seq)的升级版本。
random.sample(population, k)
随机取样(数据量小)
>>> random.sample('abaifhaoaoghaowe',4)['a', 'o', 'o', 'w']>>> random.sample('abaifhaoaoghaowe',4)['e', 'a', 'o', 'b']>>> random.sample('abaifhaoaoghaowe',4)['h', 'i', 'a', 'w']>>>
random.shuffle(x[, random])
打乱序列
不同的分布模式
不同分布模式的取样方法这里不再说了,感兴趣的可以上网查阅资料,或者留言,下期更新。
随机数例子
>>> import random>>> random.seed()>>> print(random.seed())None>>> >>> s = random.getstate()>>> s(3, (2147483648, 3974408555, 1772657792, 2739005030, 2455914616, 318246334, 2676691817, 1125013077, 2217036637, 3528505465, 2885851504, 3499645573, 2738594286, 1765167992, 615497874, 2953679498, 1265430353, 367804569, 2332181897, 1689154516, 3219312096, 4057737758, 2471609094, 3211958363, 383615958, 103572324, 240135141, 2596865565, 1922138487, 382906526, 1770940264, 4283852277, 3719673646, 1889664350, 4283069340, 3805569331, 3856338446, 2320865862, 1362308882, 1820464849, 738270275, 3294546192, 510631213, 4103713027, 511159595, 721851386, 403336915, 2664724784, 3224112726, 3158054991, 3332207060, 724472581, 2668373790, 2971067947, 425933124, 2149469404, 1904253827, 4259429900, 2749849993, 2866054638, 1082544053, 1570643949, 1864713757, 3821357104, 3940936555, 2893640957, 2604020292, 3264152668, 2203048267, 863350910, 2932268107, 3287049509, 3879511372, 2810838679, 4221877584, 1178697763, 3629777355, 704393688, 2140207989, 3649481666, 3982297605, 1546282103, 1524621584, 1483523964, 172926826, 549734183, 433995736, 2893206259, 3552026661, 2659079962, 1422407057, 1086144570, 4250689555, 681865151, 2945523940, 2137908860, 1249604978, 3996719346, 3180767693, 145765903, 665365933, 3553170463, 3784222771, 1571849076, 1791350711, 2519534020, 3541004867, 769853373, 900470723, 2795501270, 86007702, 2603990449, 3636698051, 3606678988, 2462758477, 928464745, 228157420, 2982245934, 4150373645, 501866531, 893955973, 322020625, 634754503, 3756312463, 2301305511, 1317781294, 1485469802, 1122763557, 4090164455, 2504769197, 1814131031, 110938862, 1477051766, 2186006577, 2875481754, 1052222487, 3525970891, 3331793891, 3033745183, 2393651486, 4117178113, 2725654191, 1377861989, 1154063145, 699861852, 4001254572, 3947388140, 603025980, 926647435, 1770916679, 2245145367, 127192936, 4186174479, 4265971436, 3490916425, 84692560, 3197730885, 315951055, 1893027713, 1819301021, 3931966458, 1188503979, 161902863, 4234369014, 130709027, 1471319363, 987150201, 3628647713, 3568261212, 865841199, 4098653089, 308149288, 229379238, 1658551938, 2489238392, 2342762944, 2721611965, 1163140310, 1972091668, 2017264569, 1615791261, 292151808, 2049777353, 715187917, 2819694743, 1385081733, 276978014, 2130072978, 1801286576, 4082537339, 4144803213, 4100400865, 964719970, 2604354128, 2758883990, 2007682350, 3858926394, 672136515, 2619061869, 4074804134, 3394013490, 2592495999, 423029416, 3018606236, 4224186183, 3633853270, 3804866592, 3314856141, 3336066456, 3401661630, 1489702597, 612266942, 1855988985, 680409834, 2941307052, 3381394057, 2763882383, 2620772558, 1505974291, 3365807943, 1865123986, 962071179, 1021836687, 1717811876, 1437497152, 2623084642, 119125323, 304532264, 3262462684, 3125732881, 3068060160, 2377081660, 4171385600, 1677465959, 4072144858, 1910181809, 1547699481, 3613916323, 990393893, 442355077, 3100275299, 1284186239, 3641072139, 3769925517, 2681154275, 3049934421, 2821596189, 4067027369, 4133598662, 1563622642, 907055317, 3929971031, 1514316925, 2517205566, 26853105, 377920368, 1422048382, 3718339826, 3649087869, 3908297641, 814003444, 343427640, 1330837705, 774237640, 3183476589, 2217865728, 3343329202, 1735416571, 983082083, 2572590333, 1618399009, 1449876562, 626659221, 1682185639, 3747436781, 197716930, 1705959748, 1429594111, 896174907, 215968728, 2152471307, 2305689078, 185255619, 3123301928, 3223102890, 3430690104, 949793814, 2350489550, 1240791867, 3048241627, 2953237536, 2798619253, 3096570784, 1753352306, 2460230167, 3999759272, 1136821228, 805637797, 3233947561, 2092263638, 3987257466, 811317329, 2254235399, 2722880200, 857123538, 2457350773, 700410680, 1421405564, 3299936742, 3161165872, 2691370732, 2314004631, 1104283260, 4257511207, 2139980789, 3603585511, 2582910239, 1368297385, 2568706295, 4159104912, 3345319920, 3156360163, 347474174, 2525411234, 3829238494, 381825852, 3519842940, 2806781378, 1898566662, 971207260, 505069735, 2981479797, 1275700979, 151553705, 2556678568, 1400491402, 247044755, 4242135762, 2875103431, 1832315835, 1763272794, 3989244475, 4114328906, 431056563, 1160223563, 799710518, 2788083921, 2573803803, 3877700859, 405535783, 1103030043, 1439724236, 1460807956, 2700383034, 3813908778, 2374128023, 2216232604, 1283617611, 328081826, 3909971045, 1261412265, 1791789873, 3724849414, 3469575636, 3288456509, 3433575287, 2393643586, 1583097612, 1373303426, 2450233287, 708684902, 4011613136, 722738252, 3231768277, 3176280425, 2744042689, 2174954913, 2378086529, 2714937873, 4058978735, 2426421908, 405236634, 4067172446, 3558211990, 3075928471, 2181485023, 3592429059, 2384649065, 1552943154, 615764360, 2479087517, 2131964921, 717374207, 2436432088, 650177291, 733652034, 2516532483, 639476426, 1498150838, 1522882765, 1464181518, 1096357980, 932546468, 3536003408, 151257933, 55856926, 3987537128, 1155216769, 1942618639, 1218722715, 2123540558, 1824948693, 4144044623, 3126143463, 1744179555, 106027300, 442143182, 4032173176, 1955311436, 117510346, 1138647893, 1108044180, 2116946878, 2804302205, 1229955847, 3354213539, 2893370366, 993242572, 3366020675, 2361067302, 12572148, 522107815, 4293714528, 48069514, 267334258, 1434832227, 337150237, 2698230081, 417242265, 3700758011, 1481193901, 3344995914, 1132926755, 1626462441, 2901175731, 535977058, 420287799, 3297048494, 2593695230, 1849112782, 3618698890, 3803283782, 25930130, 4259882057, 2366714283, 1889246669, 3695284613, 4205011284, 351714638, 294781808, 2935016662, 1909150549, 1391557859, 1483463498, 3889285316, 3860451518, 26039512, 1104989192, 313558360, 500463719, 2639853192, 164274941, 293937850, 1352697122, 2769952987, 1797358417, 2831975787, 378528057, 34002646, 342456898, 1625886675, 3449160310, 2560672322, 1105365230, 712266506, 3798774888, 4162560492, 1968468909, 1215420724, 3923811883, 1319146376, 2404365614, 1622188516, 3291387852, 707576538, 847418560, 2134271160, 2474597601, 1627219697, 3571107712, 3603715540, 4158080684, 2369598612, 362003559, 4109930800, 3565956963, 109567446, 2632099264, 235578716, 3719458027, 3550795022, 584656779, 1273739317, 1006067099, 369011135, 3401133211, 608488175, 3294686820, 390418897, 2163057449, 1675417340, 1895569715, 3238818110, 524239383, 3203843404, 1720687542, 2054556076, 60246950, 1132118658, 4039648196, 208244539, 2122703008, 1000360889, 2159130598, 2690936011, 3310851886, 780391606, 3832956266, 1431929950, 964999964, 1802261099, 3689067445, 556638752, 1618371817, 161568683, 859348889, 3094477738, 1670267323, 707084560, 2548640309, 1453860328, 2011667554, 2729784038, 2161017318, 3803517153, 3209019831, 3019714126, 2739364738, 721178057, 1616027758, 1910866702, 3463830141, 1533342956, 3319322746, 2218253345, 2849209419, 1660895694, 1761998435, 2268379769, 1811611048, 107529578, 3667409580, 2194313102, 384265450, 2025035646, 1781459502, 3551011104, 4206325442, 3953292236, 729424202, 1958618370, 649505126, 1179100644, 536580358, 2094245005, 3831015498, 2681805929, 1884631379, 3288458788, 1848675984, 2734577232, 4249973157, 520681187, 487349185, 1215714249, 2974300064, 4271479881, 781054912, 2936577733, 2722721833, 4051220506, 891548730, 3034676367, 3230041260, 3784341500, 92731979, 3026772680, 31286667, 168701141, 2270979572, 399179390, 1106812427, 255171695, 589742291, 2573121693, 3684406448, 1387959638, 2059778078, 2521953512, 1249468160, 2818752085, 983610453, 385905284, 1797528100, 624), None)>>> random.random()0.45065990573108816>>> random.random()0.7442767884225795>>> random.uniform(1, 9)1.9111392082420569>>> random.uniform(1, 9)7.496315395432383>>> random.uniform(12, 19)12.288268990739635>>> random.uniform(12, 19)16.10171395038531>>> >>> random.randrange(1,9)5>>> random.randrange(1,9)3>>> random.randrange(1,9)4>>> >>> random.randint(1,9)3>>> random.randint(1,9)6>>> random.randint(1,9)4>>> >>> random.choice([1,2,2,3,3])1>>> random.choice([1,2,2,3,3])2>>> random.choice([1,2,2,3,3])2>>> random.choice([1,2,2,3,3])3>>> random.choice([1,2,2,3,3])3>>> random.choice([1,2,2,3,3])2>>> random.choice('1')'1'>>> random.choice('1')'1'>>> random.choice('1adafaga23rq')'g'>>> random.choice('1adafaga23rq')'a'>>> random.choice('1adafaga23rq')'1'>>>choices与sample的区别:
choices在抽取随机元素时是包含重复元素的,即:一个元素可能会被抽取多次。反之,在sample中,抽取的元素是不重复的。所以,在抽取元素大于样本集总数时,choices会继续而sample会报错。
标签: #python27升级