Explain what a Python random module is in a parse instance

Source: Internet
Author: User
This paper mainly cooperate with examples to introduce Python Random ModuleThe usage, the random module in PythonUsed to generate random numbers. Here's a look Python Random ModuleSome of the most commonly used functions in

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.

Print Random.uniform (10,20)  print random.uniform (20,10)  #----Results (different results on various machines)  #18.7356606526  # 12.5798298022

Random.randint

random.randint () is: Random.randint (A, b), which is used to generate an integer within a specified range. Where parameter A is the lower bound, parameter B is the upper bound, and the resulting
number of machines n:a <= n <= b


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

These methods are the most common in the random module, and other methods are described in the Python manual. Interested friends can find more detailed information by querying the Python manual.

The following example is placed:


Import  Random result = Random.random ()  print result #生成0-1 of the stochastic number of    print random.uniform (10,12)  # 10-12 of the random number of    print random.randint (30,50) #30-50 of the random integer    print random.randrange (10,100,2) #从10开始到100结束, in a sequence of 2 steps, Randomly select a    list = [1,2,5,6,7,8,8]  print random.choice (list)   #从序列中随机选一个        random.shuffle (list)     # Rearrange the sequence  Print List    = [1, 2, 3, 4, 5, 6, 7, 8, 9, ten]     slice = random.sample (list, 5)   #从序列中取样  p Rint Slice

Results:

0.782366976492
11.5582702631
6
7

[1, 5, 8, 7, 2, 8, 10]
[2, 9, , 7, 8]

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.