Common Treatment Import Random
1:random.random
Random.random () is used to generate a random number of 0 to 1 points: 0 <= N < 1.0
random.random ()>>>0.8281061383805107
2:random.uniform
Random.uniform (A, b), used to generate a random number of characters in a specified range, two parameters one of which is the upper bound, and the other is the lower limit. If a > B, the generated random number n:a <= n <= B. If a <b, then B <= N <= a
Import Random Print (Random.uniform (ten)) Print (Random.uniform (ten)) # 17.583185000092442 # 11.490755619099417
3:random.randint
Random.randint (A, b), used to generate an integer within a specified range. Where parameter A is the lower limit, parameter B is the upper limit, the generated random number n:a <= n <= B
Import Random Print (Random.randint ()) # generated random number N:12 <= n <= Print (Random.randint ()) # The result is always # print Random.randint (+) # The statement is wrong. Upper limit must be greater than or equal to lower
lower bound must be less than # - # -
4:random.choice
gets a random element from the sequence. Its function prototype is: Random.choice (sequence). the parameter sequence represents an ordered type.
Here's a note: sequence is not a specific type in Python, but a series of types. list, tuple, string all belong to sequence.
ImportRandomPrint(Random.choice ("Learn python"))Print(Random.choice (["Jgood"," is","a","Handsome"," Boy"]))Print(Random.choice ("Tuple","List","Dict")))#XI#Jgood#Dict
5:random.randrange
Random.randrange ([start], stop[, step]), from within the specified range, gets a random number in the collection incremented by the specified cardinality.
Random.randrange (10, 100, 2) equivalent to Random.choice (range (10, 100, 2) on the result
Import Random Print (Random.randrange, 2#结果相当于从 [10, 12, 14, 16, ... 96, 98] Gets a random number on the result with Random.choice (range (10, 100, 2) equivalent #
6:random.shuffle
Random.shuffle (x[, Random]), which is used to disrupt elements in a list.
Import= ["Python""is""Powerful "simple" "and so on... " ]random.shuffle (P)print (p)# [' is ', ' simple ', ' and so on ... ', ' Python ', ' powerful ']
7:random.sample
Random.sample (sequence, k), randomly fetching fragments of the specified length from the specified sequence. The sample function does not modify the original sequence
Import= [1, 2, 3, 4, 5, 6, 7, 8, 9, ten= random.sample (list, 5) # randomly gets 5 elements from a list as A fragment returns print ( slice)print (list) # The original sequence has not changed # [1, 3, 9, ten, 7]#[1, 2, 3, 4, 5, 6, 7, 8, 9, ten]
Knowledge Point: Python random module (get random number) common methods and usage examples