Random module and pythonrandom Module

Source: Internet
Author: User

Random module and pythonrandom Module

I. random common modules

1. random. random () randomly generates a decimal number

Print (random. random () # output 0.6060562117996784

 

2. random. randint (m, n) randomly generates an integer (including n) from m to n)

Print (random. randint (1, 5) # output 5

 

3. random. randrange (m, n) randomly generates a number from m to n, including m but not n

Print (random. randrange (1, 5) # output 3

 

4. random. smaple (source, n) randomly finds n values in the source to generate a list.

Print (random. sample (range (100), 5) # output [27, 49, 21, 81, 45]

 

Ii. string Module

2.1 string. ascii_letters # all uppercase/lowercase English letters

Letters = string. ascii_lettersprint (letters) # output abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

  

2.2 string. ascii_lowercase # All lowercase letters

2.3 string. ascii_uppercase # all uppercase letters

 

2.4 string. digit #1-9

 

2.5 string. punctuation # special characters

Sss = string. punctuationprint (sss) # output! "# $ % & '() * +,-./:; <=>? @ [\] ^ _ '{| }~

  

2.6 generate a random Verification Code

We use the random and string modules to generate a verification code containing special characters and uppercase/lowercase letters.

Import randomimport stringstr_source = {1: string. ascii_lowercase, 2: string. ascii_uppercase, 3: string. digits, 4: string. punctuation} check = [] for I in range (1, 5): y = random. sample (str_source [I], 1) check. append (y [0]) print ("". join (check) # output bV5-

  

Related Article

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.