4

Does numpy.random yield the same random numbers (given a particular seed) on different architectures assuming the versions of python and numpy are the same on both machines?

Are there any good options that are machine independent across a wide range of python/library versions?

Dan
  • 2,851
  • 3
  • 20
  • 27

1 Answers1

5

Yes. From NumPy docs:

Compatibility Guarantee A fixed seed and a fixed series of calls to ‘RandomState’ methods using the same parameters will always produce the same results up to roundoff error except when the values were incorrect. Incorrect values will be fixed and the NumPy version in which the fix was made will be noted in the relevant docstring. Extension of existing parameter ranges and the addition of new parameters is allowed as long the previous behavior remains unchanged.

smead
  • 1,768
  • 15
  • 23
  • 1
    It's unclear whether "always" means "across different machines" or "across different runs on the same machine". I'm skeptical that such a guarantee is possible across machines: some algorithms use the rejection method, making floating-point comparisons to decide when to accept. The number of values consumed from the random sequence can then depend on the platform libm details. For an example, see this code: https://github.com/numpy/numpy/blob/b94c2b01ff7ef5b8dc44726512cfa232e9054882/numpy/random/mtrand/distributions.c#L720-L742. – Mark Dickinson Mar 11 '17 at 11:55
  • 1
    I opened a bug report to get clarity on this: https://github.com/numpy/numpy/issues/8771 – Mark Dickinson Mar 11 '17 at 12:33
  • I specifically meant across different machines. I originally asked because I was getting different answers on different machines, but realized I was not calling np.random the same number of times. When I do now, I do get the same random numbers on the two machines. I would be very interested if this might not be the case on some architectures... – Dan Mar 16 '17 at 17:45
  • Anyone know if this applies to `random.random` as well? – Thomas Kimber Apr 16 '21 at 18:41
  • 1
    @ThomasKimber - https://stackoverflow.com/questions/9023660/how-to-generate-a-repeatable-random-number-sequence might be helpful. from the answers, it seems like if you seed the random number generator with the same integer, the sequence of random numbers will be the same across machines. – jkr Apr 21 '21 at 19:00