The random module in Python is used to generate a stochastic number.
Random.random
Function Prototypes random.random()
Generates a random floating-point number in the range of [0,1].
randomrandom.random()
Random.uniform
Function Prototypes random.uniform(a,b)
Generates a random floating-point number in a specified range, two parameters one is the upper limit, and the other is the lower limit. If a > B, the resulting random number range is in [B,a]. Otherwise, the range is [A, b].
import randomprint random.uniform(3,1)print random.uniform(1,3)
Random.randint
Function Prototypes random.randint(a, b)
Generates an integer within the specified range. Where parameter A is the lower limit, parameter B is the upper limit, and the resulting random number range is [A, a], where a can only be less than or equal to B.
randomrandom.randint(1,3)
Random.randrange
Function Prototypes random.randrange([start], stop[, step])
Gets a random number in the collection that increments by the specified cardinality, from within the specified range. 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.
randomrandom.randrange(1,10,2)
Random.choice
Function Prototypes random.choice(sequence)
Random.choice gets a random element from the 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, Random.choice (Range (10, 100, 2) is equivalent to Random.randrange (10, 100, 2) on the result.
import randomprint random.choice("Sin_Geek"print random.choice(["Sin_Geek""is""a""handsome""boy"])print random.choice(("Tuple""List""Dict"))
Random.shuffle
Function Prototypes random.shuffle(x[, random])
The elements in a list are scrambled.
randoms = ["Python""is""powerful""simple""and so on..."]random.shuffle(s)print s
Random.sample
Function Prototypes random.sample(sequence, k)
Gets a random fragment of the specified length from the specified sequence without modifying the original sequence.
import randomlist = [12345678910]slice = random.sample(list5) print sliceprintlist
The above methods are the most common in the random module, and other methods are described in the Python manual.
Python Module Learning Note--random