GithubHelp home page GithubHelp logo

Comments (6)

sbailey avatar sbailey commented on June 22, 2024

Well, that is irritating. Python 3 looks worse and worse...

Adam and I confirmed that numpy is reproducible across python 2.7 and 3.5, e.g.

Python 2.7.12 |Anaconda 2.0.1 (x86_64)| (default, Jul  2 2016, 17:43:17) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import numpy as np
>>> np.random.seed(2)
>>> np.random.randint(100, size=20)
array([40, 15, 72, 22, 43, 82, 75,  7, 34, 49, 95, 75, 85, 47, 63, 31, 90,
       20, 37, 39])

(py3) ~ $ python
Python 3.5.2 |Continuum Analytics, Inc.| (default, Jul  2 2016, 17:52:12) 
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> np.random.seed(2)
>>> np.random.randint(100, size=20)
array([40, 15, 72, 22, 43, 82, 75,  7, 34, 49, 95, 75, 85, 47, 63, 31, 90,
       20, 37, 39])

Another bizarre thing with the python system library random: on my laptop, the sequence I get from ipython is one element offset from the sequence I get from vanilla python:

~ $ python
Python 2.7.12 |Anaconda 2.0.1 (x86_64)| (default, Jul  2 2016, 17:43:17) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import random; random.seed(2); random.sample(range(100), 20)
[95, 94, 5, 8, 83, 73, 66, 30, 60, 58, 15, 43, 39, 72, 99, 54, 44, 26, 3, 2]

~ $ ipython
Python 2.7.12 |Anaconda 2.0.1 (x86_64)| (default, Jul  2 2016, 17:43:17) 
Type "copyright", "credits" or "license" for more information.

IPython 5.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import random

In [2]: random.seed(2)

In [3]: random.sample(range(100),20)
Out[3]: [94, 5, 8, 83, 73, 66, 30, 60, 58, 15, 43, 39, 72, 99, 54, 44, 26, 3, 2, 46]

and to continue the bizarreness, I get the python sequence from ipython if I chain the commands together in a single line:

~ $ ipython
Python 2.7.12 |Anaconda 2.0.1 (x86_64)| (default, Jul  2 2016, 17:43:17) 
Type "copyright", "credits" or "license" for more information.

IPython 5.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import random; random.seed(2); random.sample(range(100), 20)
   ...: 
Out[1]: [95, 94, 5, 8, 83, 73, 66, 30, 60, 58, 15, 43, 39, 72, 99, 54, 44, 26, 3, 2]

Guidelines:

  • don't use the python system library random
  • use numpy.random instead
  • this is crazy

from desimodel.

sbailey avatar sbailey commented on June 22, 2024

wrong button; reopening until we've confirmed that we are using numpy.random throughout.

from desimodel.

weaverba137 avatar weaverba137 commented on June 22, 2024

Further weirdness. The fundamental function of the random module is random.random(); almost all the other functions depend on it. In Python 2 & 3, it does appear to be consistent.

Python 2:

Python 2.7.12 |Continuum Analytics, Inc.| (default, Jul  2 2016, 17:43:17) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import random
>>> random.seed(2)
>>> random.random()
0.9560342718892494
>>> random.random()
0.9478274870593494
>>> random.random()
0.05655136772680869
>>> random.random()
0.08487199515892163

Python 3:

Python 3.5.2 |Continuum Analytics, Inc.| (default, Jul  2 2016, 17:52:12) 
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
>>> random.seed(2)
>>> random.random()
0.9560342718892494
>>> random.random()
0.9478274870593494
>>> random.random()
0.05655136772680869
>>> random.random()
0.08487199515892163

So maybe for random.sample() the way the list members are hashed is what matters? Or maybe changes in how range() works? I still agree with your guidelines though!

from desimodel.

weaverba137 avatar weaverba137 commented on June 22, 2024

The random.sample() algorithm requires a random integer in the range [0,n). Python 2 & 3 have different mechanisms for obtaining that random integer, and it looks like the different mechanisms call random.random() a different number of times.

from desimodel.

weaverba137 avatar weaverba137 commented on June 22, 2024

Does anyone care about this issue? We're weeks away from dropping Python 2 entirely, so it doesn't seem relevant anymore.

from desimodel.

sbailey avatar sbailey commented on June 22, 2024

Agreed. Closing this ticket, while reiterating the guidelines:

  • don't use the python system library random
  • use numpy.random instead
  • this is crazy

from desimodel.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.