Python Random Module Example

Source: Internet
Author: User
Tags function prototype shuffle

The most recent use of random numbers, the query data summarizes the Python random module (get random number) common methods and use examples.

1,random.random

Random.random () is used to generate a random number of 0 to 1 points: 0 <= N < 1.0

2,Random.uniform
Random.uniform (A, b), used to generate a random number of characters in a specified range, two parameters one of which is the upper bound, and the other is the lower limit.

If a < b, the generated random number n:b>= n >= A.

If a >b, then the generated random number n:a>= n >= B.

Print Random.uniform (10, 20)
Print Random.uniform (20, 10)
# 14.73
# 18.579

3,Random.randint
Random.randint (A, b), used to generate an integer within a specified range. Where parameter A is the lower limit, parameter B is the upper limit, the generated random number n:a <= n <= B

Print Random.randint (1, 10)

4, Random.randrange
Random.randrange ([start], stop[, step]), from within the specified range, gets a random number in the collection incremented by the specified cardinality.

such as: Random.randrange (10, 100, 2), the result is equivalent from [10, 12, 14, 16, ... 96, 98] gets a random number in the sequence.

5, Random.choice
Random.choice gets a random element from the sequence. Its function prototype is: Random.choice (sequence). The parameter sequence represents an ordered type.

Here's a note: sequence is not a specific type in Python, but a series of types. list, tuple, string all belong to sequence.

Print Random.choice ("Python")
Print Random.choice (["Jgood", "is", "a", "handsome", "boy"])
Print Random.choice (("Tuple", "List", "Dict")

6, Random.shuffle
Random.shuffle (x[, Random]), which is used to disrupt elements in a list.

Such as:

p = [' Python ', ' is ', ' powerful ', ' simple ', ' and so ' on ... ']
Random.shuffle (P)
Print P
# [' Powerful ', ' simple ', ' was ', ' Python ', ' and so on ... ']

7, Random.sample
Random.sample (sequence, k), randomly fetching fragments of the specified length from the specified sequence. The sample function does not modify the original sequence.

For example:

list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11,12]
Slice = random.sample (list, 6) # randomly fetches 6 elements from the list, returning as a fragment
Print Slice
Print List # The original sequence has not changed

Python Random Module Example

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.