Return a list of tuples that can be passed to ``random.Random.setstate``. Parameters ---------- n : int Number of tuples to return. random_state : int or ``random.Random``, optional If an int, is used to seed a new ``random.Random``. See Also --------
(
n: int, random_state: int | Random | None = None
)
| 2549 | |
| 2550 | |
| 2551 | def random_state_data_python( |
| 2552 | n: int, random_state: int | Random | None = None |
| 2553 | ) -> list[tuple[int, tuple[int, ...], None]]: |
| 2554 | """Return a list of tuples that can be passed to |
| 2555 | ``random.Random.setstate``. |
| 2556 | |
| 2557 | Parameters |
| 2558 | ---------- |
| 2559 | n : int |
| 2560 | Number of tuples to return. |
| 2561 | random_state : int or ``random.Random``, optional |
| 2562 | If an int, is used to seed a new ``random.Random``. |
| 2563 | |
| 2564 | See Also |
| 2565 | -------- |
| 2566 | dask.utils.random_state_data |
| 2567 | """ |
| 2568 | maxuint32 = 1 << 32 |
| 2569 | |
| 2570 | try: |
| 2571 | import numpy as np |
| 2572 | |
| 2573 | if isinstance(random_state, Random): |
| 2574 | random_state = random_state.randint(0, maxuint32) |
| 2575 | np_rng = np.random.default_rng(random_state) |
| 2576 | |
| 2577 | random_data = np_rng.bytes(624 * n * 4) # `n * 624` 32-bit integers |
| 2578 | arr = np.frombuffer(random_data, dtype=np.uint32).reshape((n, -1)) |
| 2579 | return [(3, tuple(row) + (624,), None) for row in np.atleast_2d(arr).tolist()] |
| 2580 | |
| 2581 | except ImportError: |
| 2582 | # Pure python (much slower) |
| 2583 | if not isinstance(random_state, Random): |
| 2584 | random_state = Random(random_state) |
| 2585 | |
| 2586 | return [ |
| 2587 | ( |
| 2588 | 3, |
| 2589 | tuple(random_state.randint(0, maxuint32) for _ in range(624)) + (624,), |
| 2590 | None, |
| 2591 | ) |
| 2592 | for _ in range(n) |
| 2593 | ] |
| 2594 | |
| 2595 | |
| 2596 | def split(seq, n): |
no test coverage detected