Random.random
Random.random () is used to generate a random number of 0 to 1 points: 0 <= N < 1.0
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 the <> a copy code code is as follows:
Print Random.uniform (10, 20)
Print Random.uniform (20, 10)
# 18.7356606526
# 12.5798298022
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
Copy the Code code as follows:
Print Random.randint (12, 20) # generated random number N:12 <= n <= 20
Print Random.randint (20, 20) # The result is always 20
# Print Random.randint (20, 10) # The statement is wrong. Lower limit must be less than upper limit
Random.randrange
Random.randrange ([start], stop[, step]), from within the specified range, gets a random number in the collection incremented by the specified cardinality. such as: Random.randrange (10, 100, 2), the result is equivalent from [10, 12, 14, 16, ... 96, 98] gets a random number in the sequence. Random.randrange (10, 100, 2) equivalent to Random.choice (range (10, 100, 2) on the result
Random.choice
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. For sequence, you can view the Python manual data Model chapter. Here are some examples of using choice:
Copy the Code code as follows:
Print Random.choice ("Learning python")
Print Random.choice (["Jgood", "is", "a", "handsome", "boy"])
Print Random.choice (("Tuple", "List", "Dict")
Random.shuffle
Random.shuffle (x[, Random]), which is used to disrupt elements in a list. Such as:
Copy the Code code as follows:
p = [' Python ', ' is ', ' powerful ', ' simple ', ' and so ' on ... ']
Random.shuffle (P)
Print P
# [' Powerful ', ' simple ', ' was ', ' Python ', ' and so on ... ']
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
Copy the Code code as follows:
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Slice = random.sample (list, 5) # randomly fetches 5 elements from the list, returning as a fragment
Print Slice
Print List # The original sequence has not changed
Random integers:
Copy the Code code as follows:
>>> Import Random
>>> Random.randint (0,99)
# 21
Randomly select even numbers between 0 and 100:
Copy the Code code as follows:
>>> Import Random
>>> random.randrange (0, 101, 2)
# 42
Random floating point number:
Copy the Code code as follows:
>>> Import Random
>>> Random.random ()
0.85415370477785668
>>> random.uniform (1, 10)
# 5.4221167969800881
Random characters:
Copy the Code code as follows:
>>> Import Random
>>> random.choice (' abcdefg&#%^*f ')
# ' d '
Select a specific number of characters in more than one character:
Copy the Code code as follows:
>>> Import Random
Random.sample (' Abcdefghij ', 3)
# [' A ', ' d ', ' B ']
Select a specific number of characters to form a new string in multiple characters:
Copy the Code code as follows:
>>> Import Random
>>> Import String
>>> String.Join (Random.sample ([' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ', ' J '], 3)). Replace ("", "" ")
# ' FIH '
Randomly pick a string:
Copy the Code code as follows:
>>> Import Random
>>> random.choice ([' Apple ', ' pear ', ' peach ', ' orange ', ' Lemon '])
# ' Lemon '
Shuffle:
Copy the Code code as follows:
>>> Import Random
>>> items = [1, 2, 3, 4, 5, 6]
>>> random.shuffle (items)
>>> Items
# [3, 2, 5, 6, 4, 1]