Create a random number generator based on an optional seed. Parameters ---------- random_state : np.random.RandomState or int or None, default=None Random state to use. if `None`, will create an unseeded random state. If `int`, creates a state using the argument as seed.
(random_state: int | np.random.RandomState | None = None)
| 6 | |
| 7 | |
| 8 | def ensure_rng(random_state: int | np.random.RandomState | None = None) -> np.random.RandomState: |
| 9 | """Create a random number generator based on an optional seed. |
| 10 | |
| 11 | Parameters |
| 12 | ---------- |
| 13 | random_state : np.random.RandomState or int or None, default=None |
| 14 | Random state to use. if `None`, will create an unseeded random state. |
| 15 | If `int`, creates a state using the argument as seed. If a |
| 16 | `np.random.RandomState` simply returns the argument. |
| 17 | |
| 18 | Returns |
| 19 | ------- |
| 20 | np.random.RandomState |
| 21 | |
| 22 | """ |
| 23 | if random_state is None: |
| 24 | random_state = np.random.RandomState() |
| 25 | elif isinstance(random_state, int): |
| 26 | random_state = np.random.RandomState(random_state) |
| 27 | elif not isinstance(random_state, np.random.RandomState): |
| 28 | error_msg = "random_state should be an instance of np.random.RandomState, an int, or None." |
| 29 | raise TypeError(error_msg) |
| 30 | return random_state |
no outgoing calls