Transferred from: https://www.cnblogs.com/chamie/p/4917820.html Random ()
Random () Method: Returns a randomly generated real number that is within the range of [0,1]
Syntax for using the random () method:
The import random #random () method is not directly accessible, you need to import the random module, and then call the method through the random static object
Random.random
The Random.random () method is used to generate a random floating-point number from 0 to 1:0<=n<1.0
>>> Import random>>> print "random ():", Random.random () random (): 0.809221478124>>> print " Random (): ", Random.random () random (): 0.877521147987
Random.uniform
Random.uniform (A, B): Used to generate a random floating-point number within a specified range, where one is the upper limit and the other is the lower limit. If a>b, the generated random number n, which is b<=n<=a; if a>b, is a<=n<=b.
>>> Import random>>> Print random.uniform (10,20) 13.2960134544>>> print random.uniform ( 20,10) 15.9038751838
Random.randint
Random.randint (A, B): Used to generate an integer within a specified range. Where parameter A is the lower bound, and parameter B is the upper bound, the generated random number n:a<=n<=b
>>> Import random>>> Print random.randint (10,20) 11>>> print random.randint (20,20) 20#print Random.randint (20,10) #该语句是错误的, lower limit must be less than upper limit
Random.randrange
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 to obtaining a random number from the [10,12,14,16,..., 96,98] sequence. Random.randrange (10,100,2) is equivalent to Random.choice (range (10,100,2)) on the result.
>>> Import random>>> Print Random.randrange (10,100,2) 72>>> print Random.choice (range ( 10,100,2)) 28>>> Print Random.choice (range (10,100,2)) 74
Random.choice
Random.choice (Sequence): The parameter sequence represents an ordered type. Sequence is not a specific type in Python, but a series of types. List,tuple, strings belong to sequence.
>>> Import random>>> Print random.choice ("Learn Python") t>>> print Random.choice (["Jgood", "is "A", "handsome", "body"]) is>>> print Random.choice (("Tuple", "List", "Dict") List
Random.shuffle
Random.shuffle (x[, Random]): Used to disrupt elements in a list.
>>> import random>>> p=[' Pyhton ', ' is ', ' powerful ', ' simple ', ' and so ' on ... ']>>> Random.shuffle (p) >>> p[' and so on ... ', ' simple ', ' powerful ', ' Pyhton ', ' is ']
Random.sample
Random.sample (sequence,k): Randomly obtains fragments of a specified length from a specified sequence, and the sample function does not modify the original sequence.
>>> Import random>>> list=[1,2,3,4,5,6,7,8,9,10]>>> a=random.sample (list,5) # Randomly get 5 elements from list, as a fragment return >>> print a[1, 6, 8, 3]>>> Print List #原有序列并没有改变 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
python--random function (random,uniform,randint,randrange,shuffle,sample)