Python random module

Source: Internet
Author: User

The random module is a module commonly used in Python to generate random numbers or random strings. For example, you want to test a login interface, you need to batch a number of user names, the user name includes uppercase, lowercase letters and numbers, each username can not be repeated, this thing can be given to Python random to achieve.

Let's talk about the basic use of random:

Importrandom,stringPrint(Random.random ())#used to generate random floating-point numbers between 0-1Print(Random.uniform (1,3))#used to generate a random floating-point number between 1-3 (within a specified range)Print(Random.randint (0,20))#used to generate a random integer between 0-20Print(Random.randrange (1,10))#used to generate a range that can be added as a step value, as followsPrint(Random.randrange (2,10,2))#random numbers between 2-10, each number is a value of 2+n*2, that is, only 2, 4, 6, 8, 10, and the result is equivalent to Random.choice (Rango ())Print(Random.choice (range (1,10,2)))#random numbers between 1-10, each number is a value of 1+n*2, that is, only 1, 3, 5, 7, 9, and the result is equivalent to Random.randrange (*,*,*)Print(Random.choice (String.ascii_lowercase))#randomly select a A-Z between a letterPrint(Random.choice (String.ascii_uppercase))#randomly select a A-Z between a letterPrint(Random.choice (String.ascii_letters))#randomly select letters between A-Z and a-Z

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.