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

Function random_state_data_python

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

2549
2550
2551def 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
2596def 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