- Bookkeeping functions:
- Functions for integers:
- Functions for sequences:
- functions generate specific real-valued distributions
- 基础函数示例:
关于随机模块的介绍。
Bookkeeping functions:
- random.seed(a =None) 初始化随机数生成器的内部状态。如果a不是None。则使用hash(a)。
>>> random.randint(1, 10)
4
>>> random.randint(1, 10)
10
>>> random.seed(3)
>>> random.randint(1, 10)
4
>>> random.seed(3)
>>> random.randint(1, 10)
4
注:
1. 如果使用相同的seed()值,则每次生成的随机数都相同。
2. 如果不设置a的值,则系统根据时间来自己选择这个值,此时每次生成的随机数因时间差异而不同。
3. 设置的seed()值仅一次有效。
- random.getstate() 返回捕获生成器当前内部状态的对象。setstate()可以传递此对象以恢复状态。
>>> random.getstate()
(3, (2147483648, 2526466655, 3786586339, ..., 1484380595, 1086155129, 3028398190, 1479684404, 4154124763, 3840228880, 1433226981, 3124645446, 4161885044, 2820592254, 624), None)
#返回的数字太多,中间用省略号代替。
- random.setstate(state) 状态可以用getstate()获得。并且setstate()恢复发生器的内部状态,这是getstate()被调用的时候。
>>> random.setstate(random.getstate())
>>> random.getstate()
(3, (2763367667, 2770700111, 1538492911, 300746257, 1773405193, 3555543106, 3299243541, 2721025337, 980043621, 1198945183, 1006743517, 3562285306, 3808873003, 1305461709, 2001113826, 624780147, 3694171727, 299921826, 991180618, 941439941, 990350655, 2913913658, 1801737446, 584396687, 1942097212, 1095826917, 1320535668, 641866155, 1121963317, 1040192121, 1145839205, 737283472, 1072671019, 1992177462, 3257155714, 912730129, 239245720, 2514899630, 4161902427, 3551709746, 3776824856, 1060850921, 952845344, 239988266, 1916737014, 2754375016, 1059052869, 249017881, 769002777, 1217423033, 710752405, 2208798212, 1453053822, 3905446270, 2093977765, 1256896720, 1393477497, 3285545986, 2244436714, 1310694355, 2707908433, 80292645, 1745761003, 2828703323, 3718380843, 390374590, 1762204048, 2766452350, 878590657, 94520433, 688624154, 1733787365, 3542313871, 2200474293, 1960069289, 4143923836, 1525255033, 2880029536, 504229378, 1224747065, 2813951533, 3403662360, 1082152490, 354955036, 2794498991, 3298992179, 1313984625, 1314645123, 1331524808, 2986927598, 4218488215, 2334156107, 3899149945, 3905016397, 572852708, 2607781308, 1215364591, 1731543795, 347683977, 3645588910, 398110563, 1487158619, 3589664964, 2936929115, 3320028524, 2146734751, 3515311316, 4165177534, 4230970933, 1909622099, 3407339293, 2517550128, 19983043, 3791416736, 1740174179, 80990948, 1637494085, 3613340499, 475723965, 1122564470, 2002133284, 3981706660, 339908777, 4176991492, 4005427012, 1536623279, 179580936, 2523866969, 613578782, 1849636533, 4090398876, 1222977311, 607608257, 4126633543, 2172370420, 4241534567, 199517855, 2397841493, 2768323879, 2822663595, 4014596423, 1695338603, 3989384012, 4147141810, 1612476854, 4230673284, 2375555878, 1279364852, 82646680, 3196618758, 2198371684, 3663309490, 329007283, 3993737810, 2229119468, 2357221901, 2122455221, 3391985928, 4069207415, 1432637526, 2095773098, 3424487211, 495267588, 1771862678, 405675476, 2463307076, 3362089354, 2443314482, 304254199, 477744890, 2716737808, 1135329638, 2294251324, 1113578384, 850271401, 298636680, 1307210047, 547595814, 537840491, 3707277571, 3549545331, 3918880730, 2050351601, 332683873, 198600857, 2437513903, 2734353551, 3253609868, 3462577838, 2691276243, 635975186, 2566395071, 1378157992, 861196598, 2506766555, 2198781380, 3299211926, 1186715436, 3319618599, 2726543423, 1584534370, 2352811234, 3590864291, 3135974566, 1174604936, 302146107, 3592316661, 65712743, 2821847022, 2117793417, 1170610389, 1064455380, 979747570, 3669620378, 132174333, 1469066589, 4199343272, 1272775077, 3405869110, 1459023941, 526088620, 2418790342, 3428017283, 2744042861, 1691311253, 2614481610, 3889306195, 3758373687, 4277189004, 382027876, 3590465791, 221404064, 2270859688, 3432970476, 2105764869, 521520105, 4066424207, 1542493447, 2891903410, 3120051839, 2602578802, 1836499075, 4186638346, 2246597272, 134368012, 2722682468, 3945864708, 3290718488, 4140175997, 2486371885, 3275611687, 4249595229, 1695380692, 3042832530, 3831179401, 3637072996, 1757846986, 2125679067, 4186712557, 457767466, 2317143992, 4168870034, 2646817510, 1797000110, 3041641497, 1905721030, 1231421870, 2190946276, 1443016358, 2683431824, 3367139812, 1894867261, 3147168864, 2851142988, 1094367093, 2016636707, 4135570452, 260131867, 3069380314, 1786086280, 2616127458, 3829305108, 609146044, 140811119, 2107076842, 2703165334, 353681630, 3319163642, 4264465289, 1304768508, 2841713513, 265415114, 3995066998, 802429083, 937022585, 3126756600, 1069219692, 3989045457, 900546268, 2757140135, 362471702, 742126703, 645039169, 2377173731, 4082115674, 1389955441, 578485632, 688849639, 3168757424, 746176353, 480080554, 596431984, 4161878216, 2239561839, 739486236, 3028938294, 2479656027, 320733835, 3943607105, 775666835, 862496466, 2000706467, 1132419426, 3652943003, 100653488, 4192212405, 3653495526, 2904154680, 3722863654, 2101727294, 2023202536, 1405823901, 730854203, 83714620, 3976174857, 665514179, 1737964151, 1861789764, 3202621381, 2002245234, 4071769231, 4136156984, 3102177145, 3558037977, 862381099, 3438375662, 4250130505, 266857121, 2658065703, 1047990633, 3114772031, 1451182746, 456771841, 1020799895, 2153507772, 2558721626, 15381373, 1616224990, 1353979970, 2523864900, 1895571099, 2529460569, 1079391613, 2797725662, 1386707540, 2461899664, 2329820151, 2720602558, 1576283050, 259417660, 1934754997, 1722806889, 1305055959, 3659501439, 3637995722, 3496934421, 2963652585, 2586383777, 2512471405, 666116960, 789122467, 2321757509, 3687306269, 2299863731, 4165316503, 1514351941, 3049750412, 77016810, 3808529842, 3678571561, 2286360474, 1614103895, 511666905, 1064563962, 4144816424, 746593396, 699533066, 4124545024, 4091186723, 2264549654, 3286486136, 1981282270, 2215462770, 508927791, 2291518795, 1127987225, 206547196, 227723979, 2594281834, 4206701511, 3874361696, 3948851178, 988711265, 3447288424, 3681697628, 228156444, 3909880712, 3767605008, 370905829, 3749531314, 588337898, 499275081, 3274934083, 1239974690, 2303024553, 3792107857, 4265255447, 3007317632, 4079732276, 1155144812, 2229068341, 3398149872, 2727445246, 3290383365, 3782283290, 244847420, 1964524056, 282334623, 940306801, 4159448142, 1087143745, 3498587530, 3366551448, 1276397160, 3134230511, 680306484, 4007119106, 3998093036, 3924256245, 3992414807, 2335906917, 3908368861, 1750239231, 2109320715, 2415964244, 1177157885, 2299334213, 1028302108, 3618756184, 1096361237, 2590885407, 1916104655, 1620782721, 287135, 3167694150, 1396203733, 1874868777, 2547537405, 330202887, 2627453111, 4000286869, 943038094, 920946903, 1820652206, 3816960831, 3425724687, 1321452128, 2414005530, 4201501203, 408965780, 920792526, 1525252820, 199658400, 521145234, 155423550, 1900952686, 3714916782, 745422650, 3534985107, 2037983151, 2735092742, 1949717487, 3367011901, 1833699842, 2001174281, 2892002254, 3743345174, 2960077659, 1223692897, 3471139141, 1053553860, 1547185176, 2003711147, 1947392251, 2600726865, 3301668407, 3912461579, 4102621881, 1286180100, 925446348, 2894831469, 1629181179, 2992385928, 80751219, 2617651843, 2122032672, 3913127653, 4124691368, 403044924, 1297409459, 940015051, 2795213305, 1805100990, 1187392562, 2064636844, 949101918, 1433926825, 1830526646, 2951725663, 2712234039, 1301837670, 3492778645, 576762201, 1647469857, 467218898, 968700083, 984778571, 2598706199, 3791736408, 3077172827, 964291410, 2096192328, 200755118, 2113639784, 2399344237, 3263686296, 974645785, 763624090, 28174050, 3626845811, 3688802691, 1658872060, 351144261, 1797602851, 3154900115, 3829906120, 3187807224, 3338951063, 1473327219, 166986291, 3436665135, 201948140, 4135985226, 1759571280, 433331147, 1767583924, 3728385567, 1283400025, 176675686, 2033898926, 3838768164, 2260843461, 3414176876, 3645310194, 2763619670, 4031919695, 2274188571, 1146430137, 2311506758, 1856938991, 3651880647, 2758407505, 2052669560, 965026531, 847542334, 3570593073, 3912633970, 474251853, 1583623646, 681886974, 2461587670, 1658824105, 1821683768, 2997200250, 113751194, 2289205174, 762822921, 1581374778, 61866448, 987786239, 4049260415, 1940136589, 2775160303, 3640718443, 260882577, 3745746502, 3796339021, 4191680814, 588421143, 1644970650, 473547164, 4041775033, 2176336873, 2875485264, 1460077070, 1483271257, 1161879079, 2624069093, 622701300, 1156175277, 2694381727, 3800003349, 1032283041, 3806356908, 1), None)
>>> random.setstate(random.getstate())
>>> random.getstate()
(3, (2763367667, 2770700111, 1538492911, 300746257, 1773405193, 3555543106, 3299243541, 2721025337, 980043621, 1198945183, 1006743517, 3562285306, 3808873003, 1305461709, 2001113826, 624780147, 3694171727, 299921826, 991180618, 941439941, 990350655, 2913913658, 1801737446, 584396687, 1942097212, 1095826917, 1320535668, 641866155, 1121963317, 1040192121, 1145839205, 737283472, 1072671019, 1992177462, 3257155714, 912730129, 239245720, 2514899630, 4161902427, 3551709746, 3776824856, 1060850921, 952845344, 239988266, 1916737014, 2754375016, 1059052869, 249017881, 769002777, 1217423033, 710752405, 2208798212, 1453053822, 3905446270, 2093977765, 1256896720, 1393477497, 3285545986, 2244436714, 1310694355, 2707908433, 80292645, 1745761003, 2828703323, 3718380843, 390374590, 1762204048, 2766452350, 878590657, 94520433, 688624154, 1733787365, 3542313871, 2200474293, 1960069289, 4143923836, 1525255033, 2880029536, 504229378, 1224747065, 2813951533, 3403662360, 1082152490, 354955036, 2794498991, 3298992179, 1313984625, 1314645123, 1331524808, 2986927598, 4218488215, 2334156107, 3899149945, 3905016397, 572852708, 2607781308, 1215364591, 1731543795, 347683977, 3645588910, 398110563, 1487158619, 3589664964, 2936929115, 3320028524, 2146734751, 3515311316, 4165177534, 4230970933, 1909622099, 3407339293, 2517550128, 19983043, 3791416736, 1740174179, 80990948, 1637494085, 3613340499, 475723965, 1122564470, 2002133284, 3981706660, 339908777, 4176991492, 4005427012, 1536623279, 179580936, 2523866969, 613578782, 1849636533, 4090398876, 1222977311, 607608257, 4126633543, 2172370420, 4241534567, 199517855, 2397841493, 2768323879, 2822663595, 4014596423, 1695338603, 3989384012, 4147141810, 1612476854, 4230673284, 2375555878, 1279364852, 82646680, 3196618758, 2198371684, 3663309490, 329007283, 3993737810, 2229119468, 2357221901, 2122455221, 3391985928, 4069207415, 1432637526, 2095773098, 3424487211, 495267588, 1771862678, 405675476, 2463307076, 3362089354, 2443314482, 304254199, 477744890, 2716737808, 1135329638, 2294251324, 1113578384, 850271401, 298636680, 1307210047, 547595814, 537840491, 3707277571, 3549545331, 3918880730, 2050351601, 332683873, 198600857, 2437513903, 2734353551, 3253609868, 3462577838, 2691276243, 635975186, 2566395071, 1378157992, 861196598, 2506766555, 2198781380, 3299211926, 1186715436, 3319618599, 2726543423, 1584534370, 2352811234, 3590864291, 3135974566, 1174604936, 302146107, 3592316661, 65712743, 2821847022, 2117793417, 1170610389, 1064455380, 979747570, 3669620378, 132174333, 1469066589, 4199343272, 1272775077, 3405869110, 1459023941, 526088620, 2418790342, 3428017283, 2744042861, 1691311253, 2614481610, 3889306195, 3758373687, 4277189004, 382027876, 3590465791, 221404064, 2270859688, 3432970476, 2105764869, 521520105, 4066424207, 1542493447, 2891903410, 3120051839, 2602578802, 1836499075, 4186638346, 2246597272, 134368012, 2722682468, 3945864708, 3290718488, 4140175997, 2486371885, 3275611687, 4249595229, 1695380692, 3042832530, 3831179401, 3637072996, 1757846986, 2125679067, 4186712557, 457767466, 2317143992, 4168870034, 2646817510, 1797000110, 3041641497, 1905721030, 1231421870, 2190946276, 1443016358, 2683431824, 3367139812, 1894867261, 3147168864, 2851142988, 1094367093, 2016636707, 4135570452, 260131867, 3069380314, 1786086280, 2616127458, 3829305108, 609146044, 140811119, 2107076842, 2703165334, 353681630, 3319163642, 4264465289, 1304768508, 2841713513, 265415114, 3995066998, 802429083, 937022585, 3126756600, 1069219692, 3989045457, 900546268, 2757140135, 362471702, 742126703, 645039169, 2377173731, 4082115674, 1389955441, 578485632, 688849639, 3168757424, 746176353, 480080554, 596431984, 4161878216, 2239561839, 739486236, 3028938294, 2479656027, 320733835, 3943607105, 775666835, 862496466, 2000706467, 1132419426, 3652943003, 100653488, 4192212405, 3653495526, 2904154680, 3722863654, 2101727294, 2023202536, 1405823901, 730854203, 83714620, 3976174857, 665514179, 1737964151, 1861789764, 3202621381, 2002245234, 4071769231, 4136156984, 3102177145, 3558037977, 862381099, 3438375662, 4250130505, 266857121, 2658065703, 1047990633, 3114772031, 1451182746, 456771841, 1020799895, 2153507772, 2558721626, 15381373, 1616224990, 1353979970, 2523864900, 1895571099, 2529460569, 1079391613, 2797725662, 1386707540, 2461899664, 2329820151, 2720602558, 1576283050, 259417660, 1934754997, 1722806889, 1305055959, 3659501439, 3637995722, 3496934421, 2963652585, 2586383777, 2512471405, 666116960, 789122467, 2321757509, 3687306269, 2299863731, 4165316503, 1514351941, 3049750412, 77016810, 3808529842, 3678571561, 2286360474, 1614103895, 511666905, 1064563962, 4144816424, 746593396, 699533066, 4124545024, 4091186723, 2264549654, 3286486136, 1981282270, 2215462770, 508927791, 2291518795, 1127987225, 206547196, 227723979, 2594281834, 4206701511, 3874361696, 3948851178, 988711265, 3447288424, 3681697628, 228156444, 3909880712, 3767605008, 370905829, 3749531314, 588337898, 499275081, 3274934083, 1239974690, 2303024553, 3792107857, 4265255447, 3007317632, 4079732276, 1155144812, 2229068341, 3398149872, 2727445246, 3290383365, 3782283290, 244847420, 1964524056, 282334623, 940306801, 4159448142, 1087143745, 3498587530, 3366551448, 1276397160, 3134230511, 680306484, 4007119106, 3998093036, 3924256245, 3992414807, 2335906917, 3908368861, 1750239231, 2109320715, 2415964244, 1177157885, 2299334213, 1028302108, 3618756184, 1096361237, 2590885407, 1916104655, 1620782721, 287135, 3167694150, 1396203733, 1874868777, 2547537405, 330202887, 2627453111, 4000286869, 943038094, 920946903, 1820652206, 3816960831, 3425724687, 1321452128, 2414005530, 4201501203, 408965780, 920792526, 1525252820, 199658400, 521145234, 155423550, 1900952686, 3714916782, 745422650, 3534985107, 2037983151, 2735092742, 1949717487, 3367011901, 1833699842, 2001174281, 2892002254, 3743345174, 2960077659, 1223692897, 3471139141, 1053553860, 1547185176, 2003711147, 1947392251, 2600726865, 3301668407, 3912461579, 4102621881, 1286180100, 925446348, 2894831469, 1629181179, 2992385928, 80751219, 2617651843, 2122032672, 3913127653, 4124691368, 403044924, 1297409459, 940015051, 2795213305, 1805100990, 1187392562, 2064636844, 949101918, 1433926825, 1830526646, 2951725663, 2712234039, 1301837670, 3492778645, 576762201, 1647469857, 467218898, 968700083, 984778571, 2598706199, 3791736408, 3077172827, 964291410, 2096192328, 200755118, 2113639784, 2399344237, 3263686296, 974645785, 763624090, 28174050, 3626845811, 3688802691, 1658872060, 351144261, 1797602851, 3154900115, 3829906120, 3187807224, 3338951063, 1473327219, 166986291, 3436665135, 201948140, 4135985226, 1759571280, 433331147, 1767583924, 3728385567, 1283400025, 176675686, 2033898926, 3838768164, 2260843461, 3414176876, 3645310194, 2763619670, 4031919695, 2274188571, 1146430137, 2311506758, 1856938991, 3651880647, 2758407505, 2052669560, 965026531, 847542334, 3570593073, 3912633970, 474251853, 1583623646, 681886974, 2461587670, 1658824105, 1821683768, 2997200250, 113751194, 2289205174, 762822921, 1581374778, 61866448, 987786239, 4049260415, 1940136589, 2775160303, 3640718443, 260882577, 3745746502, 3796339021, 4191680814, 588421143, 1644970650, 473547164, 4041775033, 2176336873, 2875485264, 1460077070, 1483271257, 1161879079, 2624069093, 622701300, 1156175277, 2694381727, 3800003349, 1032283041, 3806356908, 2), None)
- jumpahead(n) 将内部状态改为与当前状态不同且尽可能远离当前状态。n是非负整数,用于加扰当前状态向量。
注:我在自己电脑上运行这个函数会报错,暂时还没有解决这个问题。
- random.getrandbits(k) 返回带有k个随机位的长整型数据类型。此方法随 Mersenne Twister生成器一起提供,其他一些生成器也可以将其作为API的可选部分提供。
#随机返回2^k次方之中的数
>>> random.getrandbits(3)
2
>>> random.getrandbits(3)
0
>>> random.getrandbits(3)
4
>>> random.getrandbits(3)
7
>>> random.getrandbits(3)
0
>>> random.getrandbits(3)
3
>>> random.getrandbits(3)
2
>>> random.getrandbits(3)
6
>>> random.getrandbits(3)
2
>>> random.getrandbits(3)
6
Functions for integers:
- random.randrange(start, stop[, steps]) 从中返回随机选择的元素。
range(start, stop, step)
相当于choice (range(start, stop, step))
。
>>> random.randrange(1, 10, 2)
5
>>> random.randrange(1, 10, 2)
7
>>> random.randrange(1, 10, 2)
9
- random.randint (a, b) 返回一个随机整数N。满足 a<=N<=b。
>>> random.randint(1, 5)
2
>>> random.randint(1, 5)
1
>>> random.randint(1, 5)
3
>>> random.randint(1, 5)
5
Functions for sequences:
- random.choice(seq) 从非空序列seq返回一个随机元素,如果seq为空,则返回IndexError。
>>> random.choice([1, 2, 3, 4, 5, 6, 7, 8, 9])
7
>>> random.choice([1, 2, 3, 4, 5, 6, 7, 8, 9])
5
>>> random.choice([1, 2, 3, 4, 5, 6, 7, 8, 9])
4
- random.shuffle(x[, random]) 将序列x随机移动到位,可选参数random是一个0参数函数,在[0.0, 1.0]中返回随机浮点数。默认情况下是用函数random()。
>>> lis = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> random.shuffle(lis)
>>> lis
[9, 2, 8, 4, 1, 5, 6, 7, 3]
- random.sample(population, k) 返回从总体序列中选择的k长度的唯一元素列表。用于无需更换的随机抽样。返回包含来自总图的元素的新列表,同时保持原始总体不变。
>>> lis = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> random.sample(lis, 2)
[3, 8]
>>> random.sample(lis, 4)
[9, 7, 4, 5]
functions generate specific real-valued distributions
- random.random() 返回[0.0,1.0]范围内的下一个随机浮点数。
>>> random.random()
0.6974676535495787
- random.uniform(a, b) 返回随机浮点数N。满足
a <= N <= b
。等同于a + (b-a) * random()
。
>>> random.uniform(1, 5)
1.495054061405447
- random.triangular(low, high, mode) 返回一个随机浮点数N, 以便在这些边界之间使用指定的模式。low默认为0,high默认为1。满足
low <= N <= high
。
>>> random.triangular(0, 1)
0.15033981482282038
>>> random.triangular(0, 5)
3.8121824688354584
注: 这里的mode参数应该填写什么?这个问题没有弄清楚
- random.betavariate(alpha, beta) beta分布。参数需要满足条件
alpha>0
、beta>0
。返回值在0~1之间。
>>> random.betavariate(0.5, 0.5)
0.9850905532129942
>>> random.betavariate(0.5, 0.5)
0.18690963802791907
>>> random.betavariate(0.5, 0.5)
0.7430208611499349
>>> random.betavariate(0.5, 0.5)
0.1998184801120295
>>> random.betavariate(0.5, 0.5)
0.9568762290542835
- random.expovariate(lambd) 指数分布。lamnd是1.0除以所需的平均值。该平均值应该满足非零的条件。如果lambd为正,返回值的范围从0到正无穷大;如果lambd为负,则值的范围是从负无穷大到0。
>>> random.expovariate(1)
0.21962017401753797
>>> random.expovariate(-1)
-0.2449472138490246
- random.gammavariate(alpha, beta) Gamma分布(不是Gamma函数)。参数的条件是
alpha > 0
以及beta > 0
-0.2449472138490246
>>> random.gammavariate(1, 1)
1.653560996456614
- random.gauss(mu, sigma) 高斯分布。mu是平均值,sigma是标准误差。这比
normalvariate()
下面定义的函数略快。
>>> random.gauss(1, 1)
2.511694886312462
- random.lognormvariate(mu, sigma) 记录正态分布。如果你采用这个分布的自然对数,你将获得具有平均值mu和标准差sigma的正态分布。mu可以有任何值,sigma必须大于零。
>>> random.lognormvariate(1, 1)
3.954427638917181
- random.normalvariate(mu, sigma) 正态分布。mu是平均值,sigma是标准误差。
>>> random.normalvariate(0, 1)
-1.2863742572154109
- random.vonmisesvariate(mu, kappa) mu是平均角度,以弧度表示,介于0至2 * pi之间,kappa是浓度参数,必须大于或等于零。如果kappa等于零,则该分布在0至2 * pi的范围内减小到均匀的随机角度。
>>> random.vonmisesvariate(0, 1)
6.110850801610697
- random.paretovariate(alpha) 帕累托分布。alpha是形状参数。
>>> random.paretovariate(1)
1.0717043660359733
- random.weibullvariate(alpha, beta) 威布尔分布。alpha是scale参数,beta是shape参数。
>>> random.weibullvariate(0, 1)
0.0
>>> random.weibullvariate(1, 1)
0.2127376615159419
基础函数示例:
>>> random.random() # Random float x, 0.0 <= x < 1.0
0.37444887175646646
>>> random.uniform(1, 10) # Random float x, 1.0 <= x < 10.0
1.1800146073117523
>>> random.randint(1, 10) # Integer from 1 to 10, endpoints included
7
>>> random.randrange(0, 101, 2) # Even integer from 0 to 100
26
>>> random.choice('abcdefghij') # Choose a random element
'c'
>>> items = [1, 2, 3, 4, 5, 6, 7]
>>> random.shuffle(items)
>>> items
[7, 3, 2, 5, 6, 4, 1]
>>> random.sample([1, 2, 3, 4, 5], 3) # Choose 3 elements
[4, 1, 5]
参考文献:
https://docs.python.org/2/library/random.html
https://blog.csdn.net/zz2230633069/article/details/81347858