Python random Moudle

Source: Internet
Author: User
Tags shuffle

The random module in python is used to generate random numbers. The following describes the most common functions in the random module.

Random. Random

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

Random. Uniform

The prototype of the random. Uniform function is random. Uniform (a, B), which is used to generate a random number of characters in a specified range. One of the two parameters is the upper limit and the other is the lower limit. If A> B, the random number N: A <= n <= B. If a <B, B <= n <=.

Random. randint

The prototype of the random. randint () function is random. randint (a, B), which is used to generate an integer in the specified range. Where parameter A is the lower limit, parameter B is the upper limit, and the generated random number N: A <= n <= B

Random. randrange

The prototype of the random. randrange function is random. randrange ([start], stop [, step]). a random number is obtained from the set that increments by the specified base number within the specified range. For example: Random. randrange (10,100, 2), the result is equivalent to obtaining a random number from the [10, 12, 14, 16,... 96, 98] sequence. Random. randrange (10,100, 2) is equivalent to random. Choice (range (10,100, 2) in the result.

Random. Choice

Random. Choice gets a random element from the sequence. The function prototype is random. Choice (sequence ). The sequence parameter indicates an ordered type. Here you wantDescription
Below: sequence is not a specific type in Python, but a series of types. List, tuple, and string all belong to sequence. For sequence, you can view the python manual data model chapter, or refer to: http://www.17xie.com/read-37422.html
.

Random. Shuffle

The original function of random. Shuffle is random. Shuffle (X [, random]), which is used to disrupt elements in a list.

Random. Sample

The prototype of the random. sample function is random. Sample (sequence, k). Fragments of the specified length are randomly obtained from the specified sequence. The sample function does not modify the original sequence.

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.