Based on the latest updates in Random sampling the preferred way is to use Generators instead of RandomState. Refer to What's new or different to compare both approaches. One of the key changes is the difference between the slow Mersenne Twister pseudo-random number generator (RandomState
) and a stream of random bits based on different algorithms (BitGenerators
) used in the new approach (Generators
).
Otherwise, the steps for producing random numpy array is very similar:
- Initialize random generator
Instead of RandomState
you will initialize random generator. default_rng
is the recommended constructor for the random Generator, but you can ofc try another ways.
import numpy as np
rng = np.random.default_rng(42)
# rng -> Generator(PCG64)
- Generate numpy array
Instead of randint
method, there is Generator.integers method which is now the canonical way to generate integer random numbers from a discrete uniform distribution (see already mentioned What's new or different summary). Note, that endpoint=True
uses [low, high] interval for sampling instead of the default [low, high).
arr = rng.integers(-1, 1, size=10, endpoint=True)
# array([-1, 1, 0, 0, 0, 1, -1, 1, -1, -1])
As already discussed, you have to initialize random generator (or random state) every time to generate identical array. Therefore, the simplest thing is to define custom function similar to the one from @mari756h answer:
def get_array(low, high, size, random_state=42, endpoint=True):
rng = np.random.default_rng(random_state)
return rng.integers(low, high, size=size, endpoint=endpoint)
When you call the function with the same parameters you will always get the identical numpy array.
get_array(-1, 1, 10)
# array([-1, 1, 0, 0, 0, 1, -1, 1, -1, -1])
get_array(-1, 1, 10, random_state=12345) # change random state to get different array
# array([ 1, -1, 1, -1, -1, 1, 0, 1, 1, 0])
get_array(-1, 1, (2, 2), endpoint=False)
# array([[-1, 0],
# [ 0, -1]])
And for your needs you would use get_array(-1, 1, size=(100, 2000))
.