Python--random Module

Source: Internet
Author: User

Usage examples:

ImportRandom#1) Random decimalsPrint(Random.random ())#gets a decimal number greater than 0 and less than 1 random.random ()Print(Random.uniform (1, 4))#get a decimal greater than 1 less than 3#2) Random integersPrint(Random.randint (1, 9))#gets an integer greater than or equal to 1 and less than or equal to 9Print(Random.randrange (1, 9))#get an integer greater than or equal to 1 and less than 9Print(Random.randrange (1, 9, 2))#gets an odd number greater than or equal to 1 and less than 9#3) Randomly select a return valuePrint(Random.choice ([1,' at', [4, 5]]))#returns 1 or 23 or [1,5]#4) Randomly select multiple return values, the number returned is the second parameter of the functionPrint(Random.sample ([1,' at', [4, 5]],2)]#returns any 2 combinations of list elements as a list#5) Shuffle list orderLis = [1, 2, 3, 4, 5, 6]random.shuffle (LIS)#Shuffle Scramble OrderPrint(LIS) Lis1= [5, 1, 3, 7, 9]random.shuffle (LIS1)Print(LIS1)

>>>>>>>> generate random Verification code example

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.