Python:random usage

Source: Internet
Author: User

>>import Random
>>print (Random.random ()) #生成0-1 random number
>>print (Random.randint (1,7)) #生成1-7 random number, including 1 and 7
>>print (Random.randrange (1,100,4)) #生成1-100, a random number in the base of 4, such as: 1,4,8,12,16 .... such as
>>random.choice ("Hello World") #从字符串序列中随机生成一个字母, such as: H,w,r,l,e, etc.
>>random.choice ([' Hello ', ' world ', ' Hi ', ' you ', ' Java '])
>>print (Random.sample ([1,3,5,7,9,12],3)) #从序列中随机取3个数, i.e. [1,5,7]
>>print (random.sample (' Hello World ', 3))

#洗牌

>>items=[1,2,3,4,5,6,7]
>>print (items)
[1,2,3,4,5,6,7]
>>random.shuffle (items)
>>print (items)
[2,5,7,4,6,3,1]

Python:random usage

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.