The random module in Python is primarily used to generate random numbers
1 #!/usr/bin/env python2 #Coding:utf-83 4 ImportRandom One #1, Random.random () returns a random number that produces a value between 0-1 AA =random.random () - Print('A:', a) # a:0.6166193118806547 - the #2, Random.uniform (start,end) returns a random number between [Start,end] ([] Here is the range of mathematics, which means that can be equal to) -b = Random.uniform (5, 10)#here if start is larger than end, auto will turn, keep small in front - Print('B:', b) # b:9.592194042992439 - + #3, Random.randint (start,end) returns a random integer between [start,end] -c = Random.randint (5, 10)#here if start is larger than end, auto will turn, keep small in front + Print('C:', c) # C:5 A at #4, Random.randrange (start,stop,step) returns a random integer between [start,stop] tangent interval step -D = Random.randrange (10,30,2) - Print('D:', D) # d:14 -E = Random.randrange (10)#If only one value is given, it represents any positive integer from 0 to the value ([0,10]) - Print('e:', E) # E:6 - in #5. Random.choice (SEQ) returns a value randomly from the ordered sequence (list, tuple, str) SEQ -L = ['a', 1,'b', [2,3],{'K1':'v1','K2':'v2'}] tos ='Hello_world' +t = (1,'m', 4,[1,2,3],{'K1':'v1','K2':'v2'}) -f =Random.choice (L) theg =Random.choice (s) *h =Random.choice (t) $ Print('F:', f) # f:aPanax Notoginseng Print('g:', G) # G:e - Print('h:', h) # h:1 the + #6. Random.shuffle (x) randomly disrupts the position of elements in the sequence list, returning none Ai =random.shuffle (L) the Print('I:', i) # I:none + Print('L:', L) # L: [1, [2, 3], ' B ', ' a ', {' K1 ': ' v1 ', ' K2 ': ' V2 '}] - $ #7, Random.sample (seq,k) randomly obtains fragments of the specified length (not contiguous fragments) from the sequence seq. The sample function does not modify the original sequence. $j = Random.sample (L, 3)#3 elements are randomly obtained from L and returned as a fragment - Print('J', J) # J [{' K1 ': ' v1 ', ' K2 ': ' V2 '}, ' a ', [2, 3]] - Print('L:', l) # L: [1, [2, 3], ' B ', ' a ', {' K1 ': ' v1 ', ' K2 ': ' V2 '}]
Python-Common modules-random module