Python随机模块
Python的随机模块是Python的内置模块,用于生成随机数。这些是伪随机数意味着这些不是真正的随机数。该模块可用于执行随机操作,例如生成随机数、为列表或字符串随机打印值等。
示例:打印列表中的随机值
Python3
# import random
import random
# prints a random value from the list
list1 = [1, 2, 3, 4, 5, 6]
print(random.choice(list1))
Python3
import random
random.seed(5)
print(random.random())
print(random.random())
Python3
# Python3 program explaining work
# of randint() function
# import random module
import random
# Generates a random number between
# a given positive range
r1 = random.randint(5, 15)
print("Random number between 5 and 15 is % s" % (r1))
# Generates a random number between
# two given negative range
r2 = random.randint(-10, -2)
print("Random number between -10 and -2 is % d" % (r2))
Python3
# Python3 program to demonstrate
# the use of random() function .
# import random
from random import random
# Prints random item
print(random())
Python3
# Python3 program to demonstrate the use of
# choice() method
# import random
import random
# prints a random value from the list
list1 = [1, 2, 3, 4, 5, 6]
print(random.choice(list1))
# prints a random item from the string
string = "geeks"
print(random.choice(string))
# prints a random item from the tuple
tuple1 = (1, 2, 3, 4, 5)
print(random.choice(tuple1))
Python3
# import the random module
import random
# declare a list
sample_list = [1, 2, 3, 4, 5]
print("Original list : ")
print(sample_list)
# first shuffle
random.shuffle(sample_list)
print("\nAfter the first shuffle : ")
print(sample_list)
# second shuffle
random.shuffle(sample_list)
print("\nAfter the second shuffle : ")
print(sample_list)
输出:
2
如上所述,随机模块创建伪随机数。随机数取决于播种值。例如,如果播种值为 5,则以下程序的输出将始终相同。
示例:使用种子值创建随机数
蟒蛇3
import random
random.seed(5)
print(random.random())
print(random.random())
输出:
0.6229016948897019
0.7417869892607294
上述代码的输出将始终相同。因此,它不得用于加密。
让我们讨论一些由该模块执行的常见操作。
创建随机整数
random.randint() 方法用于生成给定范围之间的随机整数。
句法 :
randint(start, end)
示例:创建随机整数
蟒蛇3
# Python3 program explaining work
# of randint() function
# import random module
import random
# Generates a random number between
# a given positive range
r1 = random.randint(5, 15)
print("Random number between 5 and 15 is % s" % (r1))
# Generates a random number between
# two given negative range
r2 = random.randint(-10, -2)
print("Random number between -10 and -2 is % d" % (r2))
输出:
Random number between 5 and 15 is 7
Random number between -10 and -2 is -9
创建随机浮动
random.random() 方法用于生成 0.0 到 0.1 之间的随机整数。
句法:
random.random()
例子:
蟒蛇3
# Python3 program to demonstrate
# the use of random() function .
# import random
from random import random
# Prints random item
print(random())
输出:
0.3717933555623072
选择随机元素
random.choice()函数用于从列表、元组或字符串返回随机项。
句法:
random.choice(sequence)
示例:从列表、字符串和元组中选择随机元素
蟒蛇3
# Python3 program to demonstrate the use of
# choice() method
# import random
import random
# prints a random value from the list
list1 = [1, 2, 3, 4, 5, 6]
print(random.choice(list1))
# prints a random item from the string
string = "geeks"
print(random.choice(string))
# prints a random item from the tuple
tuple1 = (1, 2, 3, 4, 5)
print(random.choice(tuple1))
输出:
2
k
5
洗牌列表
random.shuffle() 方法用于打乱序列(列表)。洗牌意味着改变序列元素的位置。在这里,洗牌操作就位。
句法:
random.shuffle(sequence, function)
示例:混洗列表
蟒蛇3
# import the random module
import random
# declare a list
sample_list = [1, 2, 3, 4, 5]
print("Original list : ")
print(sample_list)
# first shuffle
random.shuffle(sample_list)
print("\nAfter the first shuffle : ")
print(sample_list)
# second shuffle
random.shuffle(sample_list)
print("\nAfter the second shuffle : ")
print(sample_list)
输出:
Original list :
[1, 2, 3, 4, 5]
After the first shuffle :
[4, 3, 5, 2, 1]
After the second shuffle :
[1, 3, 4, 5, 2]
随机模块中所有函数的列表
Function Name | Description |
---|---|
seed() | Initialize the random number generator |
getstate() | Returns an object with the current internal state of the random number generator |
setstate() | Used to restore the state of the random number generator back to the specified state |
getrandbits() | Return an integer with a specified number of bits |
randrange() | Returns a random number within the range |
randint() | Returns a random integer within the range |
choice() | Returns a random item from a list, tuple, or string |
choices() | Returns multiple random elements from the list with replacement |
sample() | Returns a particular length list of items chosen from the sequence |
random() | Generate random floating numbers |
uniform() | Return random floating number between two numbers both inclusive |
triangular() | Return a random floating point number within a range with a bias towards one extreme |
betavariate() | Return a random floating point number with beta distribution |
expovariate() | Return a random floating point number with exponential distribution |
gammavariate() | Return a random floating point number with gamma distribution |
gauss() | Return a random floating point number with Gaussian distribution |
lognormvariate() | Return a random floating point number with log-normal distribution |
normalvariate() | Return a random floating point number with normal distribution |
vonmisesvariate() | Return a random floating point number with von Mises distribution or circular normal distribution |
paretovariate() | Return a random floating point number with Pareto distribution |
weibullvariate() | Return a random floating point number with Weibull distribution |