Python random module

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.