4

I'm studying Python and I found the following code:

rgen = np.random.RandomState(self.random_state)

In this code, self.random_state is an int. I looked at the documentation and discovered that RandomState(int) doesn't exist as a method, but is just a "methods container".

So, how is it possible to call RandomState(self.random_state)?

honk
  • 9,137
  • 11
  • 75
  • 83
Lokty
  • 95
  • 5

1 Answers1

0

RandomState is a class and RandomState(whatever_arguments) just creates a new instance of the class RandomState.

Instance creation normally goes through __init__ (and/or __new__) which is a special method and not always seperately documented. Normally, as in this case, it's documented in the class's docstring, you already linked to the relevant documentation page which lists the parameter for the instance creation:

class numpy.random.RandomState

Container for the Mersenne Twister pseudo-random number generator.

RandomState exposes a number of methods for generating random numbers drawn from a variety of probability distributions. In addition to the distribution-specific arguments, each method takes a keyword argument size that defaults to None. If size is None, then a single value is generated and returned. If size is an integer, then a 1-D array filled with generated values is returned. If size is a tuple, then an array with that shape is filled and returned.

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.

Parameters:

seed : {None, int, array_like}, optional

Random seed used to initialize the pseudo-random number generator. Can be any integer between 0 and 2**32 - 1 inclusive, an array (or other sequence) of such integers, or None (the default). If seed is None, then RandomState will try to read data from /dev/urandom (or the Windows analogue) if available or seed from the clock otherwise.

Community
  • 1
  • 1
MSeifert
  • 145,886
  • 38
  • 333
  • 352