Python Module Learning Note--random

Source: Internet
Author: User

The random module in Python is used to generate a stochastic number.

Random.random

Function Prototypes random.random()
Generates a random floating-point number in the range of [0,1].

randomrandom.random()

Random.uniform

Function Prototypes random.uniform(a,b)
Generates a random floating-point number in a specified range, two parameters one is the upper limit, and the other is the lower limit. If a > B, the resulting random number range is in [B,a]. Otherwise, the range is [A, b].

import randomprint random.uniform(3,1)print random.uniform(1,3)

Random.randint

Function Prototypes random.randint(a, b)
Generates an integer within the specified range. Where parameter A is the lower limit, parameter B is the upper limit, and the resulting random number range is [A, a], where a can only be less than or equal to B.

randomrandom.randint(1,3)

Random.randrange

Function Prototypes random.randrange([start], stop[, step])
Gets a random number in the collection that increments by the specified cardinality, from within the specified range. 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.

randomrandom.randrange(1,10,2)

Random.choice

Function Prototypes random.choice(sequence)
Random.choice gets a random element from the 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 (Range (10, 100, 2) is equivalent to Random.randrange (10, 100, 2) on the result.

import randomprint random.choice("Sin_Geek"print random.choice(["Sin_Geek""is""a""handsome""boy"])print random.choice(("Tuple""List""Dict"))

Random.shuffle

Function Prototypes random.shuffle(x[, random])
The elements in a list are scrambled.

randoms = ["Python""is""powerful""simple""and so on..."]random.shuffle(s)print s

Random.sample

Function Prototypes random.sample(sequence, k)
Gets a random fragment of the specified length from the specified sequence without modifying the original sequence.

import randomlist = [12345678910]slice = random.sample(list5)  print sliceprintlist

The above methods are the most common in the random module, and other methods are described in the Python manual.

Python Module Learning Note--random

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.