Ben Chuanlong Du's Blog

It is never too late to learn.

Hands on the Python module random

In [1]:
import random
In [3]:
random.seed(119)
In [5]:
state = random.getstate()
state
Out[5]:
(3,
 (2147483648,
  269878886,
  4245128097,
  4235734009,
  1628035003,
  3736071739,
  2232609185,
  4218104309,
  749208924,
  3131630921,
  4235336482,
  4069230365,
  3056126436,
  78943938,
  310522869,
  1119051588,
  2490586897,
  1761488614,
  4001775166,
  1226421836,
  4107475961,
  506651018,
  4238898610,
  2177163303,
  2528355677,
  2109602410,
  1586518536,
  2586132874,
  2154168752,
  2911772244,
  624674449,
  1436708529,
  1794423932,
  1333657662,
  2223448803,
  3330312301,
  3195073251,
  2242362196,
  2218216606,
  4279646427,
  2976933400,
  3433319876,
  2043116745,
  1487847937,
  838028716,
  2415671106,
  572500974,
  366288044,
  2496794408,
  2498884159,
  2899290681,
  2962302061,
  1915754392,
  1267978807,
  2569437815,
  2827810907,
  332747384,
  2438364601,
  1686268507,
  1866061943,
  2564687767,
  4261560975,
  2130596216,
  3426504372,
  1273400017,
  589051734,
  2942921734,
  1466643156,
  3889910378,
  618486002,
  426865306,
  2077183778,
  3122231677,
  2444746171,
  1914445475,
  138591982,
  3743664922,
  707918466,
  1517310799,
  33462227,
  2781505418,
  1543132419,
  2186979852,
  1759449778,
  866010454,
  3954445035,
  3637526920,
  2691893418,
  3181937123,
  4030212213,
  3787396582,
  1594065139,
  1705756674,
  1803388650,
  431438372,
  2596821772,
  111552800,
  4028444744,
  734525657,
  3125196464,
  2364332035,
  2311717880,
  3103891412,
  3972012583,
  2428799170,
  1595564246,
  882604044,
  1546370371,
  3007564531,
  1852571986,
  2109430909,
  2615519289,
  3598891596,
  4012581470,
  1690305991,
  4020516618,
  3486553933,
  1782629284,
  893095023,
  735739295,
  3999742469,
  3635790601,
  267659871,
  1172366424,
  2372942900,
  1342245025,
  57856350,
  2944018323,
  1702632848,
  1454909031,
  3747143950,
  377354469,
  510673698,
  3724248692,
  2909447836,
  4234936612,
  1747666632,
  4162839378,
  3063404618,
  2540883442,
  3624761054,
  3531811047,
  2646608127,
  487740323,
  1584368891,
  3041308138,
  3351348822,
  925852737,
  1406598188,
  3935272973,
  3304438249,
  2955098196,
  1138135959,
  4293404414,
  1823872994,
  3497127731,
  3250284518,
  3016457285,
  3923301533,
  2026819275,
  3720343430,
  1076137131,
  1683596420,
  1828377192,
  2794683310,
  225878427,
  2457849952,
  3914676099,
  3038867552,
  3191687069,
  1109122679,
  655359855,
  2245871577,
  1788549581,
  2958760276,
  1250518514,
  3941227074,
  2065188095,
  1870051983,
  3716376097,
  2365600272,
  1235724851,
  4276251991,
  1298768619,
  2387862750,
  4273014423,
  725621998,
  733136791,
  808913408,
  2742778317,
  2893402330,
  1994515435,
  245085599,
  3031750120,
  1352771447,
  2333717888,
  4242061636,
  2149817448,
  2865172844,
  474450172,
  1086102754,
  4049792623,
  2158232857,
  87414785,
  550379959,
  798672013,
  2131376889,
  531893883,
  1134638755,
  3483273568,
  1705080150,
  2838259745,
  1093147750,
  3495355886,
  1423761815,
  4077546915,
  893952271,
  3119615264,
  3583877196,
  4282326124,
  678904619,
  133428084,
  1960070144,
  1934226800,
  3644399690,
  1004650504,
  1041469453,
  3356123992,
  2514604755,
  755986901,
  2737841875,
  2273133662,
  3679139467,
  3021113067,
  721604868,
  888056821,
  3321216100,
  4129920058,
  3006304827,
  603568598,
  3877046857,
  2457888652,
  3890742943,
  3897359508,
  3051946879,
  2861694894,
  1309833166,
  2677718541,
  778587344,
  3366809691,
  3173326742,
  4250088172,
  2307980530,
  463652531,
  1199639800,
  1365843888,
  2992237849,
  974374490,
  3799450644,
  2901338366,
  3383289165,
  2699893890,
  3635319766,
  3976395521,
  1101169556,
  897813298,
  964375003,
  2202517232,
  3041850732,
  2035094866,
  2656538909,
  69282735,
  3639983577,
  1394956440,
  384355224,
  95995419,
  3729306921,
  1872914267,
  1106196096,
  839072210,
  2351292720,
  180126407,
  1735749949,
  2148362444,
  1486288905,
  3568914563,
  3623911115,
  1927915955,
  3493765605,
  3237040314,
  334934257,
  3809764722,
  108824022,
  1368083369,
  4198086426,
  3327916681,
  1614601846,
  3442568991,
  872721743,
  3449489393,
  209486898,
  2380382038,
  3837263636,
  1594280862,
  2741645027,
  1617522776,
  3486057770,
  2340300446,
  4025752274,
  2576597087,
  2529973873,
  2546479907,
  3532788512,
  817773642,
  977261242,
  646726189,
  2051980404,
  542295188,
  2648027929,
  2586905974,
  1877949009,
  1267434358,
  1793891551,
  1045805355,
  3943687216,
  4170344096,
  1864118313,
  2003248018,
  705188447,
  567815184,
  568820583,
  857352673,
  2937132641,
  2440253476,
  1901121110,
  2086154269,
  2420988724,
  2945750966,
  1554970248,
  660852922,
  1036727488,
  4189293345,
  3593373682,
  4069856863,
  2409912011,
  2448858330,
  1467801486,
  3989773719,
  970008544,
  3237286185,
  1987549759,
  1699533530,
  3757022306,
  1324119456,
  1808418494,
  1653929533,
  483892585,
  3105916176,
  1796247083,
  34406789,
  1709932650,
  358574770,
  31709439,
  1506673876,
  213882225,
  2646243589,
  2598478767,
  202717474,
  3719302306,
  2954216803,
  2629407644,
  3911959923,
  3630875841,
  2573909271,
  1955640710,
  701656055,
  3970265897,
  1228803984,
  679795869,
  766354336,
  2067658100,
  2818515340,
  513432574,
  2379774094,
  1909152309,
  894926481,
  2226539155,
  3580260008,
  2134454769,
  719188110,
  4227177775,
  2011995992,
  1058232086,
  2862634744,
  1848065852,
  2695106079,
  4065077290,
  3606209957,
  1969368365,
  307157172,
  3138366147,
  3911357753,
  805828463,
  778870964,
  2984082052,
  1768448184,
  4071636385,
  372719536,
  19538805,
  3900743690,
  94310385,
  1498951476,
  2023959741,
  1387096365,
  1341502695,
  2159944347,
  951601119,
  2619221470,
  132791625,
  3225820599,
  3140094611,
  779206894,
  1923147146,
  770220000,
  2258537245,
  2312466788,
  2519161337,
  3325184067,
  3273672387,
  1635511035,
  1369068329,
  1130586517,
  2160172867,
  1210160746,
  2510680485,
  3376698368,
  317128549,
  1956552102,
  3775236810,
  2622724786,
  1494253642,
  1595131381,
  931929183,
  2779577300,
  446041980,
  3401909709,
  3364860192,
  803795151,
  997660503,
  778230222,
  2305155312,
  3017246488,
  3191903354,
  2818247044,
  3541610918,
  826416341,
  2002049107,
  1488032790,
  2438700215,
  541322412,
  370329949,
  2348829556,
  3804211664,
  921620186,
  3077537250,
  3993112472,
  167675300,
  4103233999,
  464908388,
  3118116292,
  3959413546,
  506599281,
  3786560428,
  1147385651,
  2942945808,
  2772832724,
  2425586918,
  2441627814,
  3271447510,
  3512329005,
  1509902970,
  2399344904,
  2869522344,
  791748619,
  277146437,
  2662717538,
  467430938,
  2545864491,
  3347108537,
  1776837922,
  2856040361,
  3309500440,
  1197410200,
  733386277,
  1689330951,
  3157765728,
  2112841674,
  2061223388,
  2909096791,
  2433347592,
  416330188,
  1552003093,
  143352128,
  223005376,
  1613899640,
  2355073526,
  3311258140,
  692268009,
  441287588,
  2321896642,
  265309511,
  1452287866,
  3525717779,
  3533297582,
  2694574469,
  2886606757,
  512082482,
  2174365307,
  664648096,
  4009891677,
  2605228345,
  694997548,
  3082037939,
  767499365,
  1129949002,
  398428547,
  874006391,
  4196239257,
  780849823,
  3476316346,
  4285143352,
  1491577120,
  3079993742,
  670502467,
  3650077863,
  3687317103,
  1052902758,
  3410659437,
  2694855197,
  47612544,
  4159263952,
  3196862633,
  1062862551,
  2830194415,
  1091411478,
  1750913506,
  1339141048,
  580932228,
  4061999071,
  1994591537,
  1202803904,
  1444174119,
  2935141355,
  2640535784,
  4029532072,
  3845315982,
  1944907639,
  1731884288,
  261130046,
  4162195944,
  2021860959,
  69836411,
  2639164403,
  3390677493,
  3702306539,
  2280928619,
  473415905,
  474581717,
  1807778936,
  840918505,
  3088494703,
  3427131231,
  300793361,
  2043245815,
  558526100,
  1267585738,
  4062486642,
  3670879251,
  3067955786,
  3476202697,
  1797700072,
  2380675055,
  3142791321,
  3472589336,
  768540856,
  1850833670,
  3534020137,
  300140552,
  3789341485,
  140383164,
  1679795586,
  1692206860,
  3450400996,
  3311366614,
  3120748493,
  1619092454,
  4210272846,
  152756034,
  80864552,
  979556427,
  91986379,
  2564476611,
  3213761721,
  142966709,
  2434400588,
  2369867826,
  3284563935,
  2997053004,
  3939392073,
  2776151123,
  1932926108,
  1706380926,
  2122745967,
  1537351750,
  3267341883,
  669343852,
  3303925042,
  556900065,
  3145373515,
  949462581,
  584763988,
  4053132517,
  213221980,
  3018439101,
  624),
 None)
In [6]:
type(state)
Out[6]:
tuple
In [7]:
len(state)
Out[7]:
3
In [8]:
state[0]
Out[8]:
3
In [10]:
state[1]
Out[10]:
(2147483648,
 269878886,
 4245128097,
 4235734009,
 1628035003,
 3736071739,
 2232609185,
 4218104309,
 749208924,
 3131630921,
 4235336482,
 4069230365,
 3056126436,
 78943938,
 310522869,
 1119051588,
 2490586897,
 1761488614,
 4001775166,
 1226421836,
 4107475961,
 506651018,
 4238898610,
 2177163303,
 2528355677,
 2109602410,
 1586518536,
 2586132874,
 2154168752,
 2911772244,
 624674449,
 1436708529,
 1794423932,
 1333657662,
 2223448803,
 3330312301,
 3195073251,
 2242362196,
 2218216606,
 4279646427,
 2976933400,
 3433319876,
 2043116745,
 1487847937,
 838028716,
 2415671106,
 572500974,
 366288044,
 2496794408,
 2498884159,
 2899290681,
 2962302061,
 1915754392,
 1267978807,
 2569437815,
 2827810907,
 332747384,
 2438364601,
 1686268507,
 1866061943,
 2564687767,
 4261560975,
 2130596216,
 3426504372,
 1273400017,
 589051734,
 2942921734,
 1466643156,
 3889910378,
 618486002,
 426865306,
 2077183778,
 3122231677,
 2444746171,
 1914445475,
 138591982,
 3743664922,
 707918466,
 1517310799,
 33462227,
 2781505418,
 1543132419,
 2186979852,
 1759449778,
 866010454,
 3954445035,
 3637526920,
 2691893418,
 3181937123,
 4030212213,
 3787396582,
 1594065139,
 1705756674,
 1803388650,
 431438372,
 2596821772,
 111552800,
 4028444744,
 734525657,
 3125196464,
 2364332035,
 2311717880,
 3103891412,
 3972012583,
 2428799170,
 1595564246,
 882604044,
 1546370371,
 3007564531,
 1852571986,
 2109430909,
 2615519289,
 3598891596,
 4012581470,
 1690305991,
 4020516618,
 3486553933,
 1782629284,
 893095023,
 735739295,
 3999742469,
 3635790601,
 267659871,
 1172366424,
 2372942900,
 1342245025,
 57856350,
 2944018323,
 1702632848,
 1454909031,
 3747143950,
 377354469,
 510673698,
 3724248692,
 2909447836,
 4234936612,
 1747666632,
 4162839378,
 3063404618,
 2540883442,
 3624761054,
 3531811047,
 2646608127,
 487740323,
 1584368891,
 3041308138,
 3351348822,
 925852737,
 1406598188,
 3935272973,
 3304438249,
 2955098196,
 1138135959,
 4293404414,
 1823872994,
 3497127731,
 3250284518,
 3016457285,
 3923301533,
 2026819275,
 3720343430,
 1076137131,
 1683596420,
 1828377192,
 2794683310,
 225878427,
 2457849952,
 3914676099,
 3038867552,
 3191687069,
 1109122679,
 655359855,
 2245871577,
 1788549581,
 2958760276,
 1250518514,
 3941227074,
 2065188095,
 1870051983,
 3716376097,
 2365600272,
 1235724851,
 4276251991,
 1298768619,
 2387862750,
 4273014423,
 725621998,
 733136791,
 808913408,
 2742778317,
 2893402330,
 1994515435,
 245085599,
 3031750120,
 1352771447,
 2333717888,
 4242061636,
 2149817448,
 2865172844,
 474450172,
 1086102754,
 4049792623,
 2158232857,
 87414785,
 550379959,
 798672013,
 2131376889,
 531893883,
 1134638755,
 3483273568,
 1705080150,
 2838259745,
 1093147750,
 3495355886,
 1423761815,
 4077546915,
 893952271,
 3119615264,
 3583877196,
 4282326124,
 678904619,
 133428084,
 1960070144,
 1934226800,
 3644399690,
 1004650504,
 1041469453,
 3356123992,
 2514604755,
 755986901,
 2737841875,
 2273133662,
 3679139467,
 3021113067,
 721604868,
 888056821,
 3321216100,
 4129920058,
 3006304827,
 603568598,
 3877046857,
 2457888652,
 3890742943,
 3897359508,
 3051946879,
 2861694894,
 1309833166,
 2677718541,
 778587344,
 3366809691,
 3173326742,
 4250088172,
 2307980530,
 463652531,
 1199639800,
 1365843888,
 2992237849,
 974374490,
 3799450644,
 2901338366,
 3383289165,
 2699893890,
 3635319766,
 3976395521,
 1101169556,
 897813298,
 964375003,
 2202517232,
 3041850732,
 2035094866,
 2656538909,
 69282735,
 3639983577,
 1394956440,
 384355224,
 95995419,
 3729306921,
 1872914267,
 1106196096,
 839072210,
 2351292720,
 180126407,
 1735749949,
 2148362444,
 1486288905,
 3568914563,
 3623911115,
 1927915955,
 3493765605,
 3237040314,
 334934257,
 3809764722,
 108824022,
 1368083369,
 4198086426,
 3327916681,
 1614601846,
 3442568991,
 872721743,
 3449489393,
 209486898,
 2380382038,
 3837263636,
 1594280862,
 2741645027,
 1617522776,
 3486057770,
 2340300446,
 4025752274,
 2576597087,
 2529973873,
 2546479907,
 3532788512,
 817773642,
 977261242,
 646726189,
 2051980404,
 542295188,
 2648027929,
 2586905974,
 1877949009,
 1267434358,
 1793891551,
 1045805355,
 3943687216,
 4170344096,
 1864118313,
 2003248018,
 705188447,
 567815184,
 568820583,
 857352673,
 2937132641,
 2440253476,
 1901121110,
 2086154269,
 2420988724,
 2945750966,
 1554970248,
 660852922,
 1036727488,
 4189293345,
 3593373682,
 4069856863,
 2409912011,
 2448858330,
 1467801486,
 3989773719,
 970008544,
 3237286185,
 1987549759,
 1699533530,
 3757022306,
 1324119456,
 1808418494,
 1653929533,
 483892585,
 3105916176,
 1796247083,
 34406789,
 1709932650,
 358574770,
 31709439,
 1506673876,
 213882225,
 2646243589,
 2598478767,
 202717474,
 3719302306,
 2954216803,
 2629407644,
 3911959923,
 3630875841,
 2573909271,
 1955640710,
 701656055,
 3970265897,
 1228803984,
 679795869,
 766354336,
 2067658100,
 2818515340,
 513432574,
 2379774094,
 1909152309,
 894926481,
 2226539155,
 3580260008,
 2134454769,
 719188110,
 4227177775,
 2011995992,
 1058232086,
 2862634744,
 1848065852,
 2695106079,
 4065077290,
 3606209957,
 1969368365,
 307157172,
 3138366147,
 3911357753,
 805828463,
 778870964,
 2984082052,
 1768448184,
 4071636385,
 372719536,
 19538805,
 3900743690,
 94310385,
 1498951476,
 2023959741,
 1387096365,
 1341502695,
 2159944347,
 951601119,
 2619221470,
 132791625,
 3225820599,
 3140094611,
 779206894,
 1923147146,
 770220000,
 2258537245,
 2312466788,
 2519161337,
 3325184067,
 3273672387,
 1635511035,
 1369068329,
 1130586517,
 2160172867,
 1210160746,
 2510680485,
 3376698368,
 317128549,
 1956552102,
 3775236810,
 2622724786,
 1494253642,
 1595131381,
 931929183,
 2779577300,
 446041980,
 3401909709,
 3364860192,
 803795151,
 997660503,
 778230222,
 2305155312,
 3017246488,
 3191903354,
 2818247044,
 3541610918,
 826416341,
 2002049107,
 1488032790,
 2438700215,
 541322412,
 370329949,
 2348829556,
 3804211664,
 921620186,
 3077537250,
 3993112472,
 167675300,
 4103233999,
 464908388,
 3118116292,
 3959413546,
 506599281,
 3786560428,
 1147385651,
 2942945808,
 2772832724,
 2425586918,
 2441627814,
 3271447510,
 3512329005,
 1509902970,
 2399344904,
 2869522344,
 791748619,
 277146437,
 2662717538,
 467430938,
 2545864491,
 3347108537,
 1776837922,
 2856040361,
 3309500440,
 1197410200,
 733386277,
 1689330951,
 3157765728,
 2112841674,
 2061223388,
 2909096791,
 2433347592,
 416330188,
 1552003093,
 143352128,
 223005376,
 1613899640,
 2355073526,
 3311258140,
 692268009,
 441287588,
 2321896642,
 265309511,
 1452287866,
 3525717779,
 3533297582,
 2694574469,
 2886606757,
 512082482,
 2174365307,
 664648096,
 4009891677,
 2605228345,
 694997548,
 3082037939,
 767499365,
 1129949002,
 398428547,
 874006391,
 4196239257,
 780849823,
 3476316346,
 4285143352,
 1491577120,
 3079993742,
 670502467,
 3650077863,
 3687317103,
 1052902758,
 3410659437,
 2694855197,
 47612544,
 4159263952,
 3196862633,
 1062862551,
 2830194415,
 1091411478,
 1750913506,
 1339141048,
 580932228,
 4061999071,
 1994591537,
 1202803904,
 1444174119,
 2935141355,
 2640535784,
 4029532072,
 3845315982,
 1944907639,
 1731884288,
 261130046,
 4162195944,
 2021860959,
 69836411,
 2639164403,
 3390677493,
 3702306539,
 2280928619,
 473415905,
 474581717,
 1807778936,
 840918505,
 3088494703,
 3427131231,
 300793361,
 2043245815,
 558526100,
 1267585738,
 4062486642,
 3670879251,
 3067955786,
 3476202697,
 1797700072,
 2380675055,
 3142791321,
 3472589336,
 768540856,
 1850833670,
 3534020137,
 300140552,
 3789341485,
 140383164,
 1679795586,
 1692206860,
 3450400996,
 3311366614,
 3120748493,
 1619092454,
 4210272846,
 152756034,
 80864552,
 979556427,
 91986379,
 2564476611,
 3213761721,
 142966709,
 2434400588,
 2369867826,
 3284563935,
 2997053004,
 3939392073,
 2776151123,
 1932926108,
 1706380926,
 2122745967,
 1537351750,
 3267341883,
 669343852,
 3303925042,
 556900065,
 3145373515,
 949462581,
 584763988,
 4053132517,
 213221980,
 3018439101,
 624)
In [11]:
state[2]
In [12]:
random.setstate(state)

random.randrange

Return a randomly selected element from range(start, stop, step). This is equivalent to choice(range(start, stop, step)), but doesn’t actually build a range object.

The positional argument pattern matches that of range(). Keyword arguments should not be used because the function may use them in unexpected ways.

In [15]:
random.randrange(3)
Out[15]:
2
In [19]:
random.randrange(3)
Out[19]:
0
In [20]:
random.randrange(5, 7)
Out[20]:
5
In [21]:
random.randrange(5, 7)
Out[21]:
6

random.randint

Return a random integer N such that a <= N <= b (inclusive on both sides). Alias for randrange(a, b+1).

In [22]:
random.randint(3, 5)
Out[22]:
5

random.choice

Return a random element from the non-empty sequence seq. If seq is empty, raises IndexError.

In [23]:
arr = [1, 3, 6, 1000]
In [24]:
random.choice(arr)
Out[24]:
3
In [25]:
random.choice(arr)
Out[25]:
3
In [26]:
random.choice(arr)
Out[26]:
6

random.choices

Return a k sized list of elements chosen from the population with replacement. If the population is empty, raises IndexError.

In [27]:
random.choices(arr, k=9)
Out[27]:
[1000, 3, 3, 1, 6, 1000, 6, 1, 6]

random.shuffle

Shuffle the sequence x in place.

The optional argument random is a 0-argument function returning a random float in [0.0, 1.0); by default, this is the function random().

To shuffle an immutable sequence and return a new shuffled list, use sample(x, k=len(x)) instead.

Note that even for small len(x), the total number of permutations of x can quickly grow larger than the period of most random number generators. This implies that most permutations of a long sequence can never be generated. For example, a sequence of length 2080 is the largest that can fit within the period of the Mersenne Twister random number generator.

In [31]:
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
random.shuffle(arr)
arr
Out[31]:
[7, 9, 3, 5, 6, 8, 2, 4, 1]

random.sample

Return a k length list of unique elements chosen from the population sequence or set. Used for random sampling without replacement.

Returns a new list containing elements from the population while leaving the original population unchanged. The resulting list is in selection order so that all sub-slices will also be valid random samples. This allows raffle winners (the sample) to be partitioned into grand prize and second place winners (the subslices).

Members of the population need not be hashable or unique. If the population contains repeats, then each occurrence is a possible selection in the sample.

To choose a sample from a range of integers, use a range() object as an argument. This is especially fast and space efficient for sampling from a large population: sample(range(10000000), k=60).

If the sample size is larger than the population size, a ValueError is raised.

In [33]:
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
random.sample(arr, 5)
Out[33]:
[2, 1, 9, 6, 8]
In [13]:
random.random()
Out[13]:
0.9263128585070141
In [14]:
random.random()
Out[14]:
0.17981894864531622

random.uniform

In [34]:
random.uniform(0, 1)
Out[34]:
0.9816941036148611
In [35]:
random.uniform(10, 100)
Out[35]:
64.25889308957005
In [ ]:
 

Comments