"Reprint" Python module-random generation module

Source: Internet
Author: User
Tags function prototype shuffle

http://blog.csdn.net/pipisorry/article/details/39086463

random number seed

The random number of the same number of random objects with the same amount of seed is exactly the same, and the same number is generated randomly.

Random.seed (1)

So Random.randint (0,6, (4,5)) produces the same random matrix of 4*5 every time.

Introduction to seeds See [Java-Common functions random function]

Python standard library random module

(Generate random number module)

Random.random ()

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

Random.uniform (A, B)

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. If a > B, the generated random number n:b <= n <= A. If a <b, then a <= n <= b.

1 print random.uniform (  2)print random.uniform (ten)   3 # ----Results (different results on the machine)   4 # 18.7356606526   5 # 12.5798298022
Random.randint (A, B)

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

Note:a = [Random.randint (0) for __ in range ] #生成100个指定范围内的整数

Print random.randint (+)  #   generated random number N:12 <= n <=print Random.randint (s) # the   result  is always#print Random.randint (Ten)  # The statement is incorrect. The lower limit must be less than the upper limit. 
Random.randrange ([start], stop[, step])

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 (Sequence)

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, or refer to: http://www.17xie.com/read-37422.html. Here are some examples of using choice:

PrintRandom.choice ("Learn python")   PrintRandom.choice (["Jgood"," is","a","Handsome"," Boy"])  PrintRandom.choice (("Tuple","List","Dict"))
Random.shuffle (x[, Random])

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  #  ----results (results may differ on different machines.)  #  [' powerful ', ' simple ', ' is ', ' Python ' , ' and so on ... ']  
Random.sample (sequence, k)

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.

list = [1, 2, 3, 4, 5, 6, 7, 8, 9, ten]  = random.sample (list, 5)  # randomly gets 5 elements from the list, returning as a fragment 
    Print

Note: These methods are most commonly used in the random module, and other methods are described in the Python manual.


Ref:the Python Standard Library

Random distribution functions in the Python standard library

NumPy Library-random number module Numpy.random

"Reprint" Python module-random generation 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.