📜  Python中的随机数

📅  最后修改于: 2022-05-13 01:55:29.544000             🧑  作者: Mango

Python中的随机数

Python定义了一组函数,用于通过random 模块生成或操作随机数。 random 模块中的函数依赖于伪随机数生成器函数random()生成一个介于 0.0 和 1.0 之间的随机浮点数。这些特定类型的函数用于许多游戏、彩票或任何需要生成随机数的应用程序。

随机数运算

1.选择() : -choice() 是 Python 编程语言中的一个内置函数,Python从列表、元组或字符串中返回一个随机项。

例子:

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 = "striver"
print(random.choice(string))


Python3
# Python code to demonstrate the working of
# choice() and randrange()
 
# importing "random" for random operations
import random
 
# using choice() to generate a random number from a
# given list of numbers.
print("A random number from list is : ", end="")
print(random.choice([1, 4, 8, 10, 3]))
 
# using randrange() to generate in range from 20
# to 50. The last parameter 3 is step size to skip
# three numbers when selecting.
print("A random number from range is : ", end="")
print(random.randrange(20, 50, 3))


Python3
# Python code to demonstrate the working of
# random() and seed()
 
# importing "random" for random operations
import random
 
# using random() to generate a random number
# between 0 and 1
print("A random number between 0 and 1 is : ", end="")
print(random.random())
 
# using seed() to seed a random number
random.seed(5)
 
# printing mapped random number
print("The mapped random number with 5 is : ", end="")
print(random.random())
 
# using seed() to seed different random number
random.seed(7)
 
# printing mapped random number
print("The mapped random number with 7 is : ", end="")
print(random.random())
 
# using seed() to seed to 5 again
random.seed(5)
 
# printing mapped random number
print("The mapped random number with 5 is : ", end="")
print(random.random())
 
# using seed() to seed to 7 again
random.seed(7)
 
# printing mapped random number
print("The mapped random number with 7 is : ", end="")
print(random.random())


Python3
# import the random module
import random
 
 
# declare a list
sample_list = ['A', 'B', 'C', 'D', 'E']
 
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)


Python3
# Python code to demonstrate the working of
# shuffle() and uniform()
 
# importing "random" for random operations
import random
 
# Initializing list
li = [1, 4, 5, 10, 2]
 
# Printing list before shuffling
print("The list before shuffling is : ", end="")
for i in range(0, len(li)):
    print(li[i], end=" ")
print("\r")
 
# using shuffle() to shuffle the list
random.shuffle(li)
 
# Printing list after shuffling
print("The list after shuffling is : ", end="")
for i in range(0, len(li)):
    print(li[i], end=" ")
print("\r")
 
# using uniform() to generate random floating number in range
# prints number between 5 and 10
print("The random floating point number between 5 and 10 is : ", end="")
print(random.uniform(5, 10))


输出:

5
t

2. randrange(beg, end, step) :- random 模块提供了一个函数,可以从指定范围内生成随机数,并且还允许包含步骤的空间,称为randrange()。

例子:

Python3

# Python code to demonstrate the working of
# choice() and randrange()
 
# importing "random" for random operations
import random
 
# using choice() to generate a random number from a
# given list of numbers.
print("A random number from list is : ", end="")
print(random.choice([1, 4, 8, 10, 3]))
 
# using randrange() to generate in range from 20
# to 50. The last parameter 3 is step size to skip
# three numbers when selecting.
print("A random number from range is : ", end="")
print(random.randrange(20, 50, 3))

输出:

A random number from list is : 4
A random number from range is : 41

3. random() :- 该方法用于生成一个小于 1 且大于等于 0 的浮点随机数。

4. seed() :-种子函数用于保存一个随机函数的状态,以便它可以在同一台机器或不同机器上多次执行代码时生成一些随机数(针对特定的种子值)。种子值是生成器生成的先前值编号。第一次没有先前值时,它使用当前系统时间。

例子:

Python3

# Python code to demonstrate the working of
# random() and seed()
 
# importing "random" for random operations
import random
 
# using random() to generate a random number
# between 0 and 1
print("A random number between 0 and 1 is : ", end="")
print(random.random())
 
# using seed() to seed a random number
random.seed(5)
 
# printing mapped random number
print("The mapped random number with 5 is : ", end="")
print(random.random())
 
# using seed() to seed different random number
random.seed(7)
 
# printing mapped random number
print("The mapped random number with 7 is : ", end="")
print(random.random())
 
# using seed() to seed to 5 again
random.seed(5)
 
# printing mapped random number
print("The mapped random number with 5 is : ", end="")
print(random.random())
 
# using seed() to seed to 7 again
random.seed(7)
 
# printing mapped random number
print("The mapped random number with 7 is : ", end="")
print(random.random())

输出:

5. shuffle() :-用于打乱序列(列表)。改组意味着改变序列元素的位置。在这里,洗牌操作就位

例子:

Python3

# import the random module
import random
 
 
# declare a list
sample_list = ['A', 'B', 'C', 'D', 'E']
 
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 : 
['A', 'B', 'C', 'D', 'E']

After the first shuffle : 
['A', 'B', 'E', 'C', 'D']

After the second shuffle : 
['C', 'E', 'B', 'D', 'A']

6. uniform(a, b) :- 该函数用于在其参数中提到的数字之间生成一个浮点随机数它有两个参数,下限(包含在生成中)和上限(不包含在生成中)。

Python3

# Python code to demonstrate the working of
# shuffle() and uniform()
 
# importing "random" for random operations
import random
 
# Initializing list
li = [1, 4, 5, 10, 2]
 
# Printing list before shuffling
print("The list before shuffling is : ", end="")
for i in range(0, len(li)):
    print(li[i], end=" ")
print("\r")
 
# using shuffle() to shuffle the list
random.shuffle(li)
 
# Printing list after shuffling
print("The list after shuffling is : ", end="")
for i in range(0, len(li)):
    print(li[i], end=" ")
print("\r")
 
# using uniform() to generate random floating number in range
# prints number between 5 and 10
print("The random floating point number between 5 and 10 is : ", end="")
print(random.uniform(5, 10))

输出: