0

I am not very familiar with python. I have searched a lot and I want to exactly transfer below line to python:

% MATLAB code
rand('state',sum(100*clock))

I know that I should use np.random.seed() and np.random.RandomState()

but I was not able how to choose and using them to have exactly that equivalent. Thanks

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
zeinab
  • 9
  • 4
  • Why assume that there is an exact equivalent? Unless the underlying random number generators are implemented identically, this is sort of like asking for an apple which is an exact equivalent of an orange. What is wrong with numpy's way of seeding? You seemed a bit vague on that? Note that it automatically seeds without you needing to do anything. – John Coleman Apr 01 '17 at 18:31
  • This might help: http://stackoverflow.com/a/40808941/4996248 – John Coleman Apr 01 '17 at 18:40
  • thanks a lot, I know that we should not use exact equivalent, but I did not know how to use np.random.seed() and np.random.RandomState() to make that meanng. so you mean just using np.random.seed() is enough? we should not define state? – zeinab Apr 01 '17 at 19:12
  • Enough for what? If you simply want to seed from the system clock -- do nothing at all. `numpy` does it for you automatically. The only time you would explicitly seed is if you want to reproduce exactly the same sequence of random numbers, typically just for debugging purposes. – John Coleman Apr 01 '17 at 19:14
  • I got it. Thanks a lot – zeinab Apr 01 '17 at 19:18

1 Answers1

0

You don't need such a thing in Python. Unlike MATLAB which uses the same fixed random seed by default, Python automatically sets a new random seed each time, either by system-provided randomness or by the system clock depending on the platform. You only need to manually set the seed if you want to use the same seed each time. This is covered in the documentation:

random.seed(a=None, version=2) If a is omitted or None, the current system time is used. If randomness sources are provided by the operating system, they are used instead of the system time (see the os.urandom() function for details on availability).

If you want to manually reset the seed to a new random value (which you only need to do if you previously set it to a fixed value), you can just do:

>>> import random
>>>
>>> random.seed()

Or for numpy:

>>> import numpy as np
>>>
>>> np.random.seed()
TheBlackCat
  • 9,791
  • 3
  • 24
  • 31