Python random module

Source: Internet
Author: User
Tags shuffle string format

Import  Random # Number Import string

Random decimals
Print (Random.random ())0.8681861054821751
Random Print in 1-5 range
Print (Random.randint (1,5))1
and Randint difference in 1-4 range random print
Print (Random.randrange (1,5))3
Randomly take 5 numbers out of this range and print them in list mode.
Print (Random.sample (5))[+, 94, +] Print (Random.sample ('abcdef', 5)) ['b' D ' ' F ' ' e ' ' C ']
Print 0-9 numbers
Print (string.digits) 0123456789
Print A-Z
Print (string.ascii_letters) abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
Randomly take out 0-9 A-Z inside random numbers printed in string format
Str_source = string.ascii_letters + string.digitsprint(". Join (Random.sample (Str_source , 5))) dzn5w
Choice Random Pass sequence primitive, string, list inside random number
Print (Random.choice ([1,3,5]))5Print(Random.choice ('helo')) e
Shuffle function
L = [1,2,3,4,5,6,7,8]print(l) [1, 2, 3, 4, 5, 6, 7, 8]random.shuffle (l)  Print(l) [6, 2, 5, 3, 4, 8, 1, 7]

  random number verification Code applet 

Import" for" in range (5):    = Random.randrange (0,5)     if current! =   i        := Chr (Random.randint (65,90)    )elif I > 2:        = Chr (Random.randint (97,122))    Else:        = Random.randint (0,9)    + = Str (temp)print(checkcode)





Python random module

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.