0

In random, seed value should be either explicitly specified or it takes previous number generated as seed value. (internet)

I have tried this, but not getting expected output. See code and comments. https://code.sololearn.com/cwK6BrA0ZBk4/?ref=app

13th Apr 2020, 4:24 PM
harshit
harshit - avatar
15 ответов
+ 2
Try to run this code, then tell me what you saw: import random random.seed(1) for i in range(10): print(random.randint(1, 1000)) print() random.seed(1) for i in range(10): print(random.randint(1, 1000))
14th Apr 2020, 6:22 PM
HonFu
HonFu - avatar
+ 2
The documentation says, if seed is not specified, current system time is used.
14th Apr 2020, 7:40 PM
HonFu
HonFu - avatar
+ 2
You probably should not use the random module if you want to get truly random numbers. As the docs say: "Warning The pseudo-random generators of this module should not be used for security purposes. For security or cryptographic uses, see the secrets module." The random module uses the mersenne twister algorithm to generate random numbers, which is not truly random. So, it can be easily predicted. See the below link: https://bugs.python.org/issue16184 "By default, Mersenne Twister initialization is done during random module import and then after 312 random.random() calls that allows attackers to predict 313 random number with 2^8 accuracy based on 1, 2, 199, 200, 511, 625 random numbers." So, what can We do? Okay there are two ways. The first one is to use the SystemRandom class from the same random module. Like this: from random import SystemRandom random = SystemRandom() p = random.choice([2,4,532,45]) # and many more... The second one is to use seed from os.urandom. like this: import random, os random.seed(int.from_bytes(os.urandom(1), "big")) These above mentioned two methods can really give you the most truly random numbers. Because they inherit from /dev/urandom which generates random number from system noise, it doesnt contain any type of vulnerabilities.
15th Apr 2020, 5:15 AM
Ishmam
Ishmam - avatar
+ 2
No, but if you provide the seed a known number then it will always produce a predictable number. Because the random function uses an algo to do the task. So the seed is behind everything. if the seed is not random then the function is not random. The main thing is that there is no need of seeding. Just let the function work in it's own way.
18th Apr 2020, 2:40 PM
Ishmam
Ishmam - avatar
+ 1
HonFu that is when you don't specify a value for the seed, as in random.seed(). If you do this, you get a different number everytime which clarifies that the system time is being used. But if you do random.seed(value), the output will be the same set of numbers no matter how many times you run it. If system time was being taken then the out would have been different.
15th Apr 2020, 4:53 AM
XXX
XXX - avatar
+ 1
Pluto{^_^} thanks. But I still doesn't get when I am using random module, why I am not getting output as per theory. Is it due to 2^8 accuracy ?
18th Apr 2020, 2:29 PM
harshit
harshit - avatar
0
You used a new seed --> 138, so it gives a different output If you use random.seed(1) it gives 138
13th Apr 2020, 4:44 PM
Justus
Justus - avatar
0
Justus But as I have used random.seed(138) I should get 583 as output as it was in the case when I have not used any seed and it was taking previous value.
13th Apr 2020, 7:01 PM
harshit
harshit - avatar
0
harshit You set a new seed value, the output will be based the seed value
13th Apr 2020, 8:11 PM
Justus
Justus - avatar
0
Justus see, random.seed(1) print(random.randint(1,1000)) ''' output is 138, now 138 should be used as next seed value if I don't explicitly specify any seed value ''' print(random.randint(1,1000)) ''' output is 583, means for seed value 138 output will be 583 ''' random.seed(138) print(random.randint(1,1000)) ''' output is 199, but how ? '''
13th Apr 2020, 11:30 PM
harshit
harshit - avatar
14th Apr 2020, 10:51 AM
harshit
harshit - avatar
0
HonFu in your example also for seed value 138(through previous value), the random number generated is 583. But if we try to explicitly specify seed(138), then the random number generated is not 583.
14th Apr 2020, 7:09 PM
harshit
harshit - avatar
0
~ swim ~ okay, but in all the articles on internet it is written that if seed value is not specified it takes previously generated random number as seed value. https://www.geeksforgeeks.org/random-seed-in-JUMP_LINK__&&__python__&&__JUMP_LINK/amp/
14th Apr 2020, 7:12 PM
harshit
harshit - avatar