Python 种子seed 您所在的位置:网站首页 python种子怎么用 Python 种子seed

Python 种子seed

#Python 种子seed| 来源: 网络整理| 查看: 265

Python 种子seed

这不是真正的随机;相反,它被用来产生伪随机值。这表明这些随机值是可以预测的。在某些情况下,random()方法会产生数字。这个数量也被称为种子值。

语法

random.seed(i, version )

参数

i: 任何用来作为产生随机整数的种子的值。

version: 一个整数,指定如何将l变成一个整数。

返回: 一个随机值。

种子函数是如何工作的

种子方法保存了随机数生成器的状态,这样生成器就可以在同一台或其他计算机上重复执行程序时创建相同的随机值(对于一个特定的种子值)。由生成器创建的前一个值数作为种子值。如果没有初始值,它就会首次采用当前系统的时间戳。

使用random.seed()函数

我们将探讨如何每次都用一个特定的种子值产生一个相同的随机数。

代码

# Python program to show how to generate the same random number using the seed function # importing the random module import random # Initializing a loop to generate multiple random numbers for i in range(6): # Specifying a seed value # Any integer value can be used in place of 10 random.seed(10) # Generating a random integer between the values 1 to 1000 and displaying those numbers print(random.randint(1, 1000)) # We will see that all the random numbers generated will be of the same value

输出

586 586 586 586 586 586

编码

# Python program to show the difference in random number generation when the seed function is used and not used # importing the random module import random print("Random numbers after specifying a unique seed value: ") # Specifying a seed value random.seed(3) # Generating a random number in the range of 1 to 1000 print(random.randint(1, 1000)) # Specifying the same seed value to get the same random number generated earlier random.seed(3) # Again generating a random number print(random.randint(1, 1000)) # If we generate a random number without using the seed function prior to the random number generator print("Random number generated without specifying that particular or any seed value: ") print(random.randint(1, 1000)) print(random.randint(1, 1000))

输出

Random numbers after specifying a unique seed value: 244 244 Random number generated without specifying that particular or any seed value: 607 558 使用Python种子与randrange函数

让我们看看如何应用 seed() 方法在指定范围内生成相同的随机整数。

代码

# Python program to how to use the seed function with randrange # importing the random module import random # Specifying the seed value random.seed(3) # Generating a random number between the range 300 to 500 print(random.randrange(300, 500)) # Again, specifying the same seed value random.seed(3) # Again generating a random number between the range 300 to 500 print(random.randrange(300, 500))

输出

360 360 使用种子函数和选择方法

我们使用 random.choice() 方法从给定的列表或集合中选择一个随机项目。通过指定一个唯一的种子值,我们可以每次都选择相同的选项。

代码

# Python program to how to use the seed function with randrange # importing the random module import random # Creating a list of integers int_ = [1, 4, 6, 8, 9, 12, 13, 15, 16] # Specifying a seed value random.seed(5) # Using the choice method to randomly choose an integer from the list of integers random_int = random.choice(int_) print("The first random integer from the list: ", random_int) # Again, specifying the same seed value random.seed(5) random_int = random.choice(int_) print ("The second random integer from the list after using the same seed value: ", random_int)

输出

The first random integer from the list: 9 The second random integer from the list after using the same seed value: 9 使用带有样本函数的随机种子函数

我们可以使用随机样本()方法从列表或序列数据类型中选择随机项目。让我们看看如何使用seed()和sample()函数,每次都从列表中获取相同的随机样本。

代码

# Python program to how to use the seed function with sample function # importing the random module import random # Creating a list of integers list_ = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] # Specifying a seed value random.seed(3) # Using the sample function sample_items = random.sample(list_, 4) print("The first sample of integers after specifying a seed value ", sample_items) # Setting the same seed value random.seed(3) # Using the sample function again sample_items = random.sample(list_, 4) print("The second sample of integers after stating the same seed value ", sample_items)

输出

The first sample of integers after specifying a seed value [4, 9, 3, 8] The second sample of integers after stating the same seed value [4, 9, 3, 8] 使用随机种子函数和洗牌函数

我们还可以结合随机模块的seed()和shuffle()方法。结合seed()和shuffle()函数的基本目标是在每次洗牌后创建相同的输出。如果我们每次执行shuffle()方法时使用精确的种子值,我们将获得相同的元素序列。也就是说,洗牌的结果总是一样的。

代码

# Python program to how to use the seed function with shuffle function # importing the random module import random # Creating a list of integers list_ = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] print("The original list is: ", list_) # Getting a copy of the list list_1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] # Specifying a seed value random.seed(3) # Using the shuffle function random.shuffle(list_) print("Shuffled list the first time", list_) # Setting the same seed value random.seed(3) # Using the sample function again random.shuffle(list_1) print("Shuffled list the second time", list_1)

输出

The original list is: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] Shuffled list the first time [2, 6, 7, 1, 0, 5, 8, 3, 9, 4] Shuffled list the second time [2, 6, 7, 1, 0, 5, 8, 3, 9, 4]

当前面的代码被执行时,第一个打印语句显示洗牌前的原始列表。我们得到了一个列表的副本,因为randome.shuffle()方法对给定的列表进行了就地洗牌。然后我们指定了一个种子值并洗了第一个列表。然后我们用同样的种子值来洗刷原始列表的副本。我们在两次洗牌中得到了相同的元素序列。

Python中的随机种子方法的用途 这被用来创建一个伪随机的加密代码。加密密钥是计算机安全的一个重要组成部分。这些类型的密匙被用来保护数据,防止不需要的互联网访问。 使用随机数有助于轻松优化代码。通过种子法,任务变得更加简单。代码的输出有时取决于输入。因此,使用随机数来评估算法会很复杂。另外,种子法用于重复创建相同的随机整数,简化了算法的测试程序。


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有