Python random number

Source: Internet
Author: User

Premise: You need to import the random module

>>>import Random

1, Random.random

Random.random () is used to generate a random character from 0 to 1 decimals: 0 <= N < 1.0

>>> random.random () # random float x,

2, Random.uniform

The function prototype for Random.uniform is: Random.uniform (A, b), which is used to generate a random number of points within a specified range of two parameters, one of which is the upper limit and the other is the lower limit. If a > B, the generated random number n:a <= n <= B. If a <b, then B <= N <= A.

>>> Random.uniform (1, ten) # random float x,

3, Random.randint

The function prototype for Random.randint () is: Random.randint (A, b), which is used to generate integers in a specified range. Where parameter A is the lower limit, parameter B is the upper limit, the generated random number n:a <= n <= B

>>> Random.randint (10, 100)

4, Random.randrange

The function prototype for Random.randrange is: Random.randrange ([start], stop[, step]), which gets a random number from the specified range, 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. Random.randrange (10, 100, 2) is equivalent to Random.choice (range (10, 100, 2) on the result.

Randomly select even numbers between 0 and 100:
>>> Import Random
>>> random.randrange (0, 101, 2)

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. For sequence, you can view the Python manual data Model chapter.

>>> random.choice (' abcdefg&#%^*f ') #随机字符
' d '

>>> random.choice ([' Apple ', ' pear ', ' peach ', ' orange ', ' Lemon ']) #随机选取字符串:
' Lemon '

6, Random.shuffle

The function prototype for random.shuffle is: 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
#结果 (because it's random, so your results may be different.) )
#[' powerful ', ' simple ', ' was ', ' Python ', ' and so on ... ']
p = [' Python ', ' is ', ' powerful ', ' simple ', ' and so ' on ... ']

7, Random.sample

The function prototype for random.sample is: random.sample (sequence, k), which randomly fetches fragments of the specified length from the specified sequence. The sample function does not modify the original sequence. If k is greater than the number of sequence elements, it will be reported

Python random number

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.