Random is a stochastic number of Python module, the main method has the following several
1, Random.randrange (start,stop,step)
#它负责给给定的范围内随机的选出一个元素
Import Random if __name__= ="__main__" :print(Random.randrange (1,3 ) #random.randrange corresponds to randomly selecting an element from a set [1,3]
2, Random.randint (star,stop)
#它是就相当于random. Randrange (start,stop+1)
3, Random.choice (seq)
Randomly select an element in the #它用于从seq
Import Random if __name__= ="__main__" :print(Random.choice ([up]))
4, random.shuffle (seq)
#它就地打乱seq的顺序, so for a sequence that does not support modification, it cannot be used as a shuffle parameter.
Import Random if __name__= ="__main__": l=[1,2,3] random.shuffle (l) Print (l)
5, Random.sample (seq,k)
#从seq中随机的选出k个元素, returns a tuple of these k elements
Import Random if __name__== "__main__": l=[1,2,3,4,5] Print (Random.sample (l,3))
6, Random.rondom ()
A random floating-point number between the #返回 [0,1]
Import Random if __name__= ="__main__" :Print( Random.random ())
7, Random.uniform (A, B)
#如果a <b then returns a floating-point number between [A, b], if A>b then returns a floating-point number between [B,a]
Import Random if __name__= ="__main__" :print(Random.uniform (1,4))
Python random module