简体   繁体   English

random.seed():它有什么作用?

[英]random.seed(): What does it do?

I am a bit confused on what random.seed() does in Python.我对random.seed()在 Python 中的作用有点困惑。 For example, why does the below trials do what they do (consistently)?例如,为什么下面的试验会(一致地)做他们所做的事情?

>>> import random
>>> random.seed(9001)
>>> random.randint(1, 10)
1
>>> random.randint(1, 10)
3
>>> random.randint(1, 10)
6
>>> random.randint(1, 10)
6
>>> random.randint(1, 10)
7

I couldn't find good documentation on this.我找不到这方面的好文档。

Pseudo-random number generators work by performing some operation on a value.伪随机数生成器通过对值执行一些操作来工作。 Generally this value is the previous number generated by the generator.通常,此值是生成器生成的前一个数字。 However, the first time you use the generator, there is no previous value.但是,第一次使用生成器时,没有以前的值。

Seeding a pseudo-random number generator gives it its first "previous" value.为伪随机数生成器做种子会为其提供第一个“前一个”值。 Each seed value will correspond to a sequence of generated values for a given random number generator.每个种子值将对应于给定随机数生成器的一系列生成值。 That is, if you provide the same seed twice, you get the same sequence of numbers twice.也就是说,如果您提供相同的种子两次,您将获得两次相同的数字序列。

Generally, you want to seed your random number generator with some value that will change each execution of the program.通常,您希望为随机数生成器设置一些值,该值会改变程序的每次执行。 For instance, the current time is a frequently-used seed.例如,当前时间是一个经常使用的种子。 The reason why this doesn't happen automatically is so that if you want, you can provide a specific seed to get a known sequence of numbers.这不会自动发生的原因是,如果您愿意,您可以提供特定的种子来获得已知的数字序列。

All the other answers don't seem to explain the use of random.seed().所有其他答案似乎都没有解释 random.seed() 的使用。 Here is a simple example ( source ):这是一个简单的例子(来源):

import random
random.seed( 3 )
print "Random number with seed 3 : ", random.random() #will generate a random number 
#if you want to use the same random number once again in your program
random.seed( 3 )
random.random()   # same random number as before
>>> random.seed(9001)   
>>> random.randint(1, 10)  
1     
>>> random.seed(9001)     
>>> random.randint(1, 10)    
1           
>>> random.seed(9001)          
>>> random.randint(1, 10)                 
1                  
>>> random.seed(9001)         
>>> random.randint(1, 10)          
1     
>>> random.seed(9002)                
>>> random.randint(1, 10)             
3

You try this.你试试这个。

Let's say 'random.seed' gives a value to random value generator ('random.randint()') which generates these values on the basis of this seed.假设 'random.seed' 为随机值生成器 ('random.randint()') 提供一个值,该生成器基于此种子生成这些值。 One of the must properties of random numbers is that they should be reproducible.随机数的必备属性之一是它们应该是可重现的。 When you put same seed, you get the same pattern of random numbers.当你放置相同的种子时,你会得到相同的随机数模式。 This way you are generating them right from the start.这样您就可以从一开始就生成它们。 You give a different seed- it starts with a different initial (above 3).你给了一个不同的种子——它以不同的首字母开头(3以上)。

Given a seed, it will generate random numbers between 1 and 10 one after another.给定一个种子,它将一个接一个地生成 1 到 10 之间的随机数。 So you assume one set of numbers for one seed value.所以你假设一个种子值有一组数字。

A random number is generated by some operation on previous value.随机数是通过对先前值的某种操作生成的。

If there is no previous value then the current time is taken as previous value automatically.如果没有以前的值,则当前时间将自动作为以前的值。 We can provide this previous value by own using random.seed(x) where x could be any number or string etc.我们可以使用random.seed(x)自己提供这个先前的值,其中x可以是任何数字或字符串等。

Hence random.random() is not actually perfect random number, it could be predicted via random.seed(x) .因此random.random()实际上并不是完美的随机数,它可以通过random.seed(x)进行预测。

import random 
random.seed(45)            #seed=45  
random.random()            #1st rand value=0.2718754143840908
0.2718754143840908  
random.random()            #2nd rand value=0.48802820785090784
0.48802820785090784  
random.seed(45)            # again reasign seed=45  
random.random()
0.2718754143840908         #matching with 1st rand value  
random.random()
0.48802820785090784        #matching with 2nd rand value

Hence, generating a random number is not actually random, because it runs on algorithms.因此,生成随机数实际上并不是随机的,因为它运行在算法上。 Algorithms always give the same output based on the same input.算法总是基于相同的输入给出相同的输出。 This means, it depends on the value of the seed.这意味着,这取决于种子的价值。 So, in order to make it more random, time is automatically assigned to seed() .因此,为了使其更加随机,时间会自动分配给seed()

Seed() can be used for later use ---

Example:
>>> import numpy as np
>>> np.random.seed(12)
>>> np.random.rand(4)
array([0.15416284, 0.7400497 , 0.26331502, 0.53373939])
>>>
>>>
>>> np.random.seed(10)
>>> np.random.rand(4)
array([0.77132064, 0.02075195, 0.63364823, 0.74880388])
>>>
>>>
>>> np.random.seed(12) # When you use same seed as before you will get same random output as before
>>> np.random.rand(4)
array([0.15416284, 0.7400497 , 0.26331502, 0.53373939])
>>>
>>>
>>> np.random.seed(10)
>>> np.random.rand(4)
array([0.77132064, 0.02075195, 0.63364823, 0.74880388])
>>>
# Simple Python program to understand random.seed() importance

import random

random.seed(10)

for i in range(5):    
    print(random.randint(1, 100))

Execute the above program multiple times...多次执行上述程序...

1st attempt: prints 5 random integers in the range of 1 - 100第一次尝试:打印 1 - 100 范围内的 5 个随机整数

2nd attempt: prints same 5 random numbers appeared in the above execution.第二次尝试:打印上述执行中出现的相同的 5 个随机数。

3rd attempt: same第三次尝试:相同

.....So on .....很快

Explanation: Every time we are running the above program we are setting seed to 10, then random generator takes this as a reference variable.解释:每次我们运行上面的程序时,我们都将种子设置为 10,然后随机生成器将其作为参考变量。 And then by doing some predefined formula, it generates a random number.然后通过执行一些预定义的公式,它会生成一个随机数。

Hence setting seed to 10 in the next execution again sets reference number to 10 and again the same behavior starts...因此,在下一次执行中将种子设置为 10 再次将参考编号设置为 10,并且再次开始相同的行为......

As soon as we reset the seed value it gives the same plants.一旦我们重置种子值,它就会给出相同的植物。

Note: Change the seed value and run the program, you'll see a different random sequence than the previous one.注意:更改种子值并运行程序,您将看到与前一个不同的随机序列。

In this case, random is actually pseudo-random.在这种情况下,随机实际上是伪随机。 Given a seed, it will generate numbers with an equal distribution.给定一个种子,它将生成具有相等分布的数字。 But with the same seed, it will generate the same number sequence every time.但是使用相同的种子,它每次都会生成相同的数字序列。 If you want it to change, you'll have to change your seed.如果你想改变它,你就必须改变你的种子。 A lot of people like to generate a seed based on the current time or something.很多人喜欢根据当前时间或其他东西生成种子。

Imho, it is used to generate same random course result when you use random.seed(samedigit) again.恕我直言,当您再次使用random.seed(samedigit)时,它用于生成相同的随机课程结果。

In [47]: random.randint(7,10)

Out[47]: 9


In [48]: random.randint(7,10)

Out[48]: 9


In [49]: random.randint(7,10)

Out[49]: 7


In [50]: random.randint(7,10)

Out[50]: 10


In [51]: random.seed(5)


In [52]: random.randint(7,10)

Out[52]: 9


In [53]: random.seed(5)


In [54]: random.randint(7,10)

Out[54]: 9

Set the seed(x) before generating a set of random numbers and use the same seed to generate the same set of random numbers.在生成一组随机数之前设置seed(x)并使用相同的种子生成相同的随机数集。 Useful in case of reproducing the issues.在重现问题的情况下很有用。

>>> from random import *
>>> seed(20)
>>> randint(1,100)
93
>>> randint(1,100)
88
>>> randint(1,100)
99
>>> seed(20)
>>> randint(1,100)
93
>>> randint(1,100)
88
>>> randint(1,100)
99
>>> 

Here is my understanding.这是我的理解。 Every time we set a seed value, a "label" or " reference" is generated.每次我们设置种子值时,都会生成一个“标签”或“引用”。 The next random.function call is attached to this "label", so next time you call the same seed value and random.function, it will give you the same result.下一个 random.function 调用附加到这个“标签”,所以下次你调用相同的种子值和 random.function 时,它会给你相同的结果。

np.random.seed( 3 )
print(np.random.randn()) # output: 1.7886284734303186

np.random.seed( 3 )
print(np.random.rand()) # different function. output: 0.5507979025745755

np.random.seed( 5 )
print(np.random.rand()) # different seed value. output: 0.22199317108973948

random.seed(a, version) in python is used to initialize the pseudo-random number generator (PRNG) . random.seed(a, version)用于初始化伪随机数生成器 (PRNG)

PRNG is algorithm that generates sequence of numbers approximating the properties of random numbers. PRNG 是生成近似随机数属性的数字序列的算法。 These random numbers can be reproduced using the seed value .可以使用种子值复制这些随机数 So, if you provide seed value, PRNG starts from an arbitrary starting state using a seed.因此,如果您提供种子值,PRNG 将从使用种子的任意起始状态开始。

Argument a is the seed value.参数a是种子值。 If the a value is None , then by default, current system time is used.如果 a 值为None ,则默认使用当前系统时间。

and version is An integer specifying how to convert the a parameter into a integer.version是一个整数,指定如何将 a 参数转换为整数。 Default value is 2.默认值为 2。

import random
random.seed(9001)
random.randint(1, 10) #this gives output of 1
# 1

If you want the same random number to be reproduced then provide the same seed again如果您想要复制相同的随机数,请再次提供相同的种子

random.seed(9001)
random.randint(1, 10) # this will give the same output of 1
# 1

If you don't provide the seed, then it generate different number and not 1 as before如果你不提供种子,那么它会生成不同的数字而不是像以前一样的 1

random.randint(1, 10) # this gives 7 without providing seed
# 7

If you provide different seed than before , then it will give you a different random number如果您提供与以前不同的种子,那么它将为您提供不同的随机数

random.seed(9002)
random.randint(1, 10) # this gives you 5 not 1
# 5

So, in summary, if you want the same random number to be reproduced, provide the seed.因此,总而言之,如果您希望复制相同的随机数,请提供种子。 Specifically, the same seed .具体来说,相同的种子

Here is a small test that demonstrates that feeding the seed() method with the same argument will cause the same pseudo-random result:这是一个小测试,它演示了使用相同的参数提供seed()方法将导致相同的伪随机结果:

# testing random.seed()

import random

def equalityCheck(l):
    state=None
    x=l[0]
    for i in l:
        if i!=x:
            state=False
            break
        else:
            state=True
    return state


l=[]

for i in range(1000):
    random.seed(10)
    l.append(random.random())

print "All elements in l are equal?",equalityCheck(l)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM