Python Random Module

Source: Internet
Author: User
Tags shuffle

Construction random is a common function in the program, Python built-in support for this, concise and efficient. This blog mainly records some of the functions commonly used in random.

random.random(): Returns a floating-point number from 0 to one that is left closed right.
Return the next random floating point number in the range [0.0, 1.0].

random.uniform(a, b): Returns a floating-point number between A and B.
Return a random floating point number n Such this a <= N <= b for a <= B and b <= N <= A for b < A.
Implementation mode: A + (b-a) * random ()

random.randint(a, b): Returns an integer between A and B, including A and B.
Return a random integer n such that a <= N <= B. Alias for Randrange (A, b+1).

random.choice(seq): Returns a random element in the sequence seq and an error if the sequence is empty.
Return a random element from the non-empty sequence seq. If seq is empty, raises indexerror.

random.randrange(stop) ,random.randrange(start, stop[, step]): Equivalent to Choice (range (start, stop, Step)). However, the Range object is not created.
Return a randomly selected element from range (start, stop, step). This is equivalent to choice (range (start, stop, step)), but doesn ' t actually build a Range object.

random.sample(population, k): Returns a list of the first k elements in the population.
Return a k length list of unique elements chosen from the population sequence or set. Used for random sampling without replacement.

random.shuffle(x[, random]): There is no return value in itself, and the action is to disrupt the elements in X.
Shuffle the sequence x in place. The optional argument random is a 0-argument function returning a random float in [0.0, 1.0); By default, the function random ().

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Python Random Module

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.