Home > Article > Backend Development > An introduction to the usage of seven common functions used by the random module in Python to generate random numbers
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:
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))
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
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('HelloWorld') 'r' >>> random.choice(['java', 'python', 'C' , 'PHP']) 'python' >>> random.choice(('list', 'tuple', 'dict')) 'tuple'
Function prototype:
random.shuffle(x[, random])
is used to shuffle the elements in a list.
>>> l = ['java', 'python', 'C' , 'PHP'] >>> random.shuffle(l) >>> l ['PHP', 'C', 'java', 'python']
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!