Python's random function

Source: Internet
Author: User

# random various ways to use import random# random generate [0.1] floating-point number print ("Random ():", Random.random ()) # randomly generates an integer between 1000-9999 and print ("Randint ( (9999): ", Random.randint (1000, 9999)) # randomly generates even print between 0-20 (" Randrange (0,, 2): ", Random.randrange (0, 21, 2)) # Randomly generates a floating-point number between 0-20 print ("Uniform (0):", Random.uniform (0, 20)) # Random selection of an element from a sequence list_string = [' A ', ' B ', ' C ', ' d ', ' E ']print ( "Choice (list):", Random.choice (list_string)) Print ("Choice (String):", Random.choice (' ABCD ')) # Random ordering of list elements List_number = [1, 2, 3, 4, 5]random.shuffle (list_number) print ("Shuffle (list):", list_number) # Randomly gets a fragment of the specified length from the specified sequence print ("sample ( Sequence): ", Random.sample (' ABCDEFG ', 2))

Operation Result:

Random (): 0.6708362810735843randint (9999): 5228randrange (0, 2): 6uniform (0): 12.767906137387294choice ( List): Achoice (String): Dshuffle (list): [1, 3, 5, 2, 4]sample (sequence): [' f ', ' G ']

Python's random function

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.