Python-random Module

Source: Internet
Author: User

Import Random
>>> random.randrange(1,10) #返回1-10之间的一个随机数,不包括10>>> random.randint(1,10) #返回1-10之间的一个随机数,包括10>>> random.randrange(0, 100, 2) #随机选取0到100间的偶数>>> random.random()  #返回一个随机浮点数>>> random.choice(‘abce3#[email protected]‘) #返回一个给定数据集合中的随机字符‘#‘>>> random.sample(‘abcdefghij‘,3)  #从多个字符中选取特定数量的字符[‘a‘, ‘d‘, ‘b‘]
Import string
>>> string.ascii_letters‘abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ‘>>> string.digits‘0123456789‘>>> string.ascii_lowercase‘abcdefghijklmnopqrstuvwxyz‘>>> string.ascii_uppercase‘ABCDEFGHIJKLMNOPQRSTUVWXYZ‘
Generate Random string
>>>import string>>>‘‘.join(random.sample(string.ascii_lowercase+string.digits,5))‘l1r0p‘>>>‘‘.join(random.sample(string.ascii_lowercase+string.digits,5))‘8vm42‘
Shuffle
>>>=list(range(10))>>> a[0123456789]>>> random.shuffle(a)>>> a[1048369572]>>>

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.