The random module of Python common modules

Source: Internet
Author: User

ImportRandomPrint(Random.random ())#randomly generates a decimal between 0-1Print(Random.randint (1,3))#randomly generates an integer between 1-3, including 1 and 3Print(Random.randrange (1,3))#randomly generates an integer greater than or equal to 1 and less than 3, excluding 3Print(Random.choice ([1,2,[3,5]))#randomly select a single or [3,5] from within the brackets .Print(Random.sample ([1,' at', [4,5]],3)]#any 3 combinations of list elementsPrint(Random.uniform (1,3))#randomly generates a decimal number greater than 1 and less than 3

Example: Generate a 4-bit verification code:

1 ImportRandom2Code_list = []3  forIinchRange (4):4NUM1 = Random.randint (0, 9)#randomly generate a 0-9 number.5str1 = Chr (Random.randint (65, 90))#randomly generate a number between 65-90 and then turn him into a letter6s =Random.choice ([num1,str1])7 Code_list.append (str (s))8Code ="'. Join (code_list)9 Print(code)

The random module of Python common modules

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.