The random module in Python is used to generate a stochastic number. Here are some of the most commonly used functions in the random module.
Random.random
Random.random () is used to generate a random number of 0 to 1 points: 0 <= N < 1.0
Import random>>> n = random.random ()print n0.904401389056>>> nn = Random.random ()print nn
Random.uniform
The function prototype for Random.uniform is: Random.uniform (A, b), which is used to generate a random number of points within a specified range of two parameters, one of which is the upper limit 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.
Print random.uniform (1,20)print random.uniform (20,10)print Random.uniform (20,10)print random.uniform (20,10)print random.uniform (20,10 )
Random.randint
The function prototype for Random.randint () is: Random.randint (A, b), which is used to generate integers in a specified range. Where parameter A is the lower limit, parameter B is the upper limit, the generated random number n:a <= n <= B
Print random.randint (10,20)print random.randint (10,200)print Random.randint (100,200)# cannot generate less than 100 and it is not possible to generate more than 200
The lower limit must be less than the upper limit. Otherwise the consequences would be unthinkable:
>>>PrintRandom.randint (1000,200) Traceback (most recent): File"<stdin>", Line 1,inch<module>File"/usr/lib/python2.7/random.py", Line 242,inchRandintreturnSelf.randrange (A, b+1) File"/usr/lib/python2.7/random.py", line 218,inchRandrangeRaiseValueError,"Empty range for Randrange () (%d,%d,%d)"%(Istart, istop, width) valueerror:empty range forRandrange () (1000,201,-799)>>>
Random.randrange
The function prototype for Random.randrange is: Random.randrange ([start], stop[, step]), which gets a random number from the specified range, 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) is equivalent to Random.choice (range (10, 100, 2) on the result.
Print Random.randrange (10,100,3)print random.randrange (10,100,3)Print Random.randrange (10,100,3)print random.randrange (10,100,2)print Random.randrange (10,100,2)
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.
>>>PrintRandom.choice ("Learning is a matter of my own 123456")1>>>PrintRandom.choice ("Learning is a matter of my own 123456")2>>>PrintRandom.choice ("Learning is a matter of my own 123456")?>>>PrintRandom.choice ("Learning is a matter of my own 123456")?>>>PrintRandom.choice ("[Learning is your own business 123456]")1>>>PrintRandom.choice ("(learning is my own business 123456)")?>>>Random.shuffle
The function prototype for random.shuffle is: random.shuffle (x[, Random]), which is used to disrupt elements in a list. Such as:
>>> list[1, 2, 3, 4, 5, 6, 7, 8, 9,,]>>> random.shuffle (list)>>&G T list[, 8, 2, 7, 4, 6, 1, 9, 5, 3]
Random.sample
The function prototype for random.sample is: random.sample (sequence, k), which randomly fetches fragments of the specified length from the specified sequence. The sample function does not modify the original sequence.
>>> list[, 8, 2, 7, 4, 6, 1, 9, 5,, 3]>>> slice = random.sample (list,5) C9>print slice[7, 6, A, 9, 1]
Python's random function