MCPcopy Create free account
hub / github.com/dask/dask / random_state_data_python

Function random_state_data_python

dask/bag/core.py:2555–2597  ·  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

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