Python's random function

Source: Internet
Author: User
Tags shuffle


# Random use of various methods
Import Random

# random generation of floating-point numbers [0.1]
Print ("Random ():", Random.random ())

# randomly generates an integer between 1000-9999
Print ("Randint (9999):", Random.randint (1000, 9999))

# randomly generates an even number between 0-20
Print ("Randrange (0, 2):", Random.randrange (0, 21, 2))

# randomly generated floating-point numbers between 0-20
Print ("Uniform (0):", Random.uniform (0, 20))

# randomly select an element from the 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 getting fragments of a specified length from a specified sequence
Print ("Sample (Sequence):", Random.sample (' ABCDEFG ', 2))

Operation Result:

Random (): 0.6708362810735843
Randint (1000, 9999): 5228
Randrange (0, 21, 2): 6
Uniform (0, 20): 12.767906137387294
Choice (list): a
Choice (string): D
Shuffle (list): [1, 3, 5, 2, 4]
Sample (Sequence): [' f ', ' G ']

Python's random function

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.