cross python version randomness
Robin Becker
robin at reportlab.com
Mon Mar 20 12:32:05 EDT 2017
Is there a way to get the same sequences of random numbers in python 2.7 and
python >= 3.3?
I notice that this simple script produces different values in python 2.7 and >=3.3
C:\code\hg-repos\reportlab>cat s.py
import sys, random
print(sys.version)
random.seed(103)
for i in range(5):
print(i, random.randint(10,25))
C:\code\hg-repos\reportlab>\python27\python s.py
2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:53:40) [MSC v.1500 64 bit (AMD64)]
0 25
1 17
2 21
3 21
4 13
C:\code\hg-repos\reportlab>\python33\python s.py
3.3.5 (v3.3.5:62cf4e77f785, Mar 9 2014, 10:35:05) [MSC v.1600 64 bit (AMD64)]
0 24
1 16
2 12
3 13
4 22
However, when I use random.random() all seems to be the same so this script
C:\code\hg-repos\reportlab>cat u.py
import sys, random
print(sys.version)
random.seed(103)
for i in range(5):
print(i, random.random())
seems to be fine
C:\code\hg-repos\reportlab>\python27\python u.py
2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:53:40) [MSC v.1500 64 bit (AMD64)]
(0, 0.9790501200727744)
(1, 0.45629827629184827)
(2, 0.7188470341002364)
(3, 0.7348862425853395)
(4, 0.21490166849706338)
C:\code\hg-repos\reportlab>\python33\python u.py
3.3.5 (v3.3.5:62cf4e77f785, Mar 9 2014, 10:35:05) [MSC v.1600 64 bit (AMD64)]
0 0.9790501200727744
1 0.45629827629184827
2 0.7188470341002364
3 0.7348862425853395
4 0.21490166849706338
presumably randint is doing something different to get its values.
--
Robin Becker
More information about the Python-list
mailing list