The random module in Python, from the lab in Capricorn

Source: Internet
Author: User
Tags function prototype shuffle

The random module in Python is used to generate a stochastic number. Here are some of the most commonly used functions in the random module.

Random.random

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

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.

    1. Print Random.uniform (ten )
    2. Print Random.uniform ( ten)
    3. #----Results (different results on the machine)
    4. #18.7356606526
    5. #12.5798298022

Print random.uniform (+) Print Random.uniform #----results (different results on various machines) #18.7356606526 #12.5798298022

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

    1. Print Random.randint ( #生成的随机数n): <= n <=
    2. Print Random.randint ( #结果永远是20 )
    3. #print Random.randint (#该语句是错误的). The lower limit must be less than the upper limit.

Print Random.randint (#生成的随机数n): <= n <= print random.randint #print random.randint (20), ) #该语句是错误的. The lower limit must be less than the upper limit.

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.

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. Here are some examples of using choice:

    1. Print Random.choice ("Learning python")
    2. Print Random.choice (["Jgood", "is", "a", "handsome", "Boy"])
    3. Print Random.choice (("Tuple", "List", "Dict")

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

Random.shuffle

The function prototype for random.shuffle is: random.shuffle (x[, Random]), which is used to disrupt elements in a list. Such as:

    1. p = [' Python ', ' is ', ' powerful ', ' simple ', ' and so ' on ... ']
    2. Random.shuffle (P)
    3. Print P
    4. #----Results (results may vary on different machines.) )
    5. #[' powerful ', ' simple ', ' was ', ' Python ', ' and so on ... ']

p = [' Python ', ' is ', ' powerful ', ' simple ', ' and so ' on ... '] Random.shuffle (p) Print P #----Results (results may differ on different machines.) #[' powerful ', ' simple ', ' was ', ' Python ', ' and so on ... ']

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.

    1. list = [1, 2, 3, 4, 5, 6, 7, 8, 9, ten]
    2. Slice = random.sample (list, 5) #从list中随机获取5个元素, returns as a fragment
    3. Print Slice
    4. Print List #原有序列并没有改变.  

Random integers:
>>> Import Random
>>> Random.randint (0,99)
21st

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

Random floating point number:
>>> Import Random
>>> Random.random ()
0.85415370477785668
>>> random.uniform (1, 10)
5.4221167969800881

Random characters:
>>> Import Random
>>> random.choice (' abcdefg&#%^*f ')
' d '

Select a specific number of characters in more than one character:
>>> Import Random
Random.sample (' Abcdefghij ', 3)
[' A ', ' d ', ' B ']

Select a specific number of characters to form a new string in multiple characters:
>>> Import Random
>>> Import String
>>> String.Join (Random.sample ([' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ', ' J '], 3)). R
Eplace ("", "" ")
' FIH '

Randomly pick a string:
>>> Import Random
>>> random.choice ([' Apple ', ' pear ', ' peach ', ' orange ', ' Lemon '])
' Lemon '

Shuffle:
>>> Import Random
>>> items = [1, 2, 3, 4, 5, 6]
>>> random.shuffle (items)
>>> Items
[3, 2, 5, 6, 4, 1]

The random module in Python, from the lab in Capricorn

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.