MCPcopy
hub / github.com/dask/dask / random_state_data_python

Function random_state_data_python

dask/bag/core.py:2563–2605  ·  view source on GitHub ↗

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
)

Source from the content-addressed store, hash-verified

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

Callers 1

random_sampleMethod · 0.85

Calls 3

RandomClass · 0.90
reshapeMethod · 0.80
randintMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…