An introduction to the usage of seven common functions used by the random module in Python to generate random numbers

不言
Release: 2018-09-26 16:02:07
Original
5307 people have browsed it

This article brings you an introduction to the usage of seven common functions used by the random module in Python to generate random numbers. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. .

The random module in Python is used to generate random numbers.

You need to import random

before using this module. Several common function usages:

1, random.random

function Prototype:

random.random()
Copy after login

is used to generate a random character number from 0 to 1: 0 <= n < 1.0

>>> random.random()0.5578093677010638
Copy after login

2, random.uniform

Function prototype:

random.uniform(a, b)
Copy after login

is used to generate a random number of character points within a specified range. One of the two parameters is the upper limit and the other is the lower limit. If a > b, the generated random number n: b <= n <= a. If a

>>> random.uniform(10, 20)
16.864972616523794
>>> random.uniform(20, 10)
10.851664722380086
Copy after login

3. random.randint

Function prototype:

random.randint(a, b)
Copy after login

is used to generate an integer within a specified range. The parameter a is the lower limit, the parameter b is the upper limit, and the generated random number n: a <= n <= b.

>>> random.randint(12, 20)
>>> random.randint(20, 20)
>>> random.randint(30, 20)  # 不能这样用,下限必须小于等于上限
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "D:\Software\Anaconda3\lib\random.py", line 221, in randint
    return self.randrange(a, b+1)
  File "D:\Software\Anaconda3\lib\random.py", line 199, in randrange
    raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width))
Copy after login


4、random.randrange

Function prototype:

random.randrange([start], stop[, step])
Copy after login

Get from the set in the specified range incremented by the specified base A random number. For example: random.randrange(10, 100, 2), the result is equivalent to obtaining a random number from the sequence [10, 12, 14, 16, … 96, 98]. random.randrange(10, 100, 2) is equivalent to random.choice(range(10, 100, 2) in terms of results.

>>> random.randrange(10, 100)
29
>>> random.randrange(10, 100, 2)
98
Copy after login

5, random.choice

Function prototype:

random.choice(sequence)
Copy after login

Get a random element from the sequence. Among them, the parameter sequence represents an ordered type. Note: sequence is not a specific type in python, but generally refers to a series of types. list, tuple, Strings all belong to sequence.

>>> random.choice(&#39;HelloWorld&#39;)
&#39;r&#39;
>>> random.choice([&#39;java&#39;, &#39;python&#39;, &#39;C&#39; , &#39;PHP&#39;])
&#39;python&#39;
>>> random.choice((&#39;list&#39;, &#39;tuple&#39;, &#39;dict&#39;))
&#39;tuple&#39;
Copy after login

6, random.shuffle

Function prototype:

random.shuffle(x[, random])
Copy after login

is used to shuffle the elements in a list.

>>> l = [&#39;java&#39;, &#39;python&#39;, &#39;C&#39; , &#39;PHP&#39;]
>>> random.shuffle(l)
>>> l
[&#39;PHP&#39;, &#39;C&#39;, &#39;java&#39;, &#39;python&#39;]
Copy after login

7. random.sample

Function prototype:

random.sample(sequence, k)
Copy after login

Randomly obtain a fragment of the specified length from the specified sequence. The sample function will not modify the original sequence.

>>> random.sample([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 5)
[7, 2, 9, 4, 1]
Copy after login

The above is the detailed content of An introduction to the usage of seven common functions used by the random module in Python to generate random numbers. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!