Home  >  Article  >  Backend Development  >  An introduction to the usage of seven common functions used by the random module in Python to generate random numbers

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

不言
不言Original
2018-09-26 16:02:075381browse

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

is used to generate a random character number from 0 to 1: 0 c52470a21e89e1ce399ef54dcfbeabb4 b, the generated random number n: b <= n <= a. If a

>>> random.uniform(10, 20)
16.864972616523794
>>> random.uniform(20, 10)
10.851664722380086

3. random.randint

Function prototype:

random.randint(a, b)

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


4、random.randrange

Function prototype:

random.randrange([start], stop[, step])

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

5, random.choice

Function prototype:

random.choice(sequence)

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;

6, random.shuffle

Function prototype:

random.shuffle(x[, random])

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;]

7. random.sample

Function prototype:

random.sample(sequence, k)

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]

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!

Statement:
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