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

Function pseudorandom

dask/utils.py:514–538  ·  view source on GitHub ↗

Pseudorandom array of integer indexes >>> pseudorandom(5, [0.5, 0.5], random_state=123) array([1, 0, 0, 1, 1], dtype=int8) >>> pseudorandom(10, [0.5, 0.2, 0.2, 0.1], random_state=5) array([0, 2, 0, 3, 0, 1, 2, 1, 0, 0], dtype=int8)

(n: int, p, random_state=None)

Source from the content-addressed store, hash-verified

512
513
514def pseudorandom(n: int, p, random_state=None):
515 """Pseudorandom array of integer indexes
516
517 >>> pseudorandom(5, [0.5, 0.5], random_state=123)
518 array([1, 0, 0, 1, 1], dtype=int8)
519
520 >>> pseudorandom(10, [0.5, 0.2, 0.2, 0.1], random_state=5)
521 array([0, 2, 0, 3, 0, 1, 2, 1, 0, 0], dtype=int8)
522 """
523 import numpy as np
524
525 p = list(p)
526 cp = np.cumsum([0] + p)
527 assert np.allclose(1, cp[-1])
528 assert len(p) < 256
529
530 if not isinstance(random_state, np.random.RandomState):
531 random_state = np.random.RandomState(random_state)
532
533 x = random_state.random_sample(n)
534 out = np.empty(n, dtype="i1")
535
536 for i, (low, high) in enumerate(zip(cp[:-1], cp[1:])):
537 out[(x >= low) & (x < high)] = i
538 return out
539
540
541def random_state_data(n: int, random_state=None) -> list:

Callers 1

pd_splitFunction · 0.90

Calls 4

random_sampleMethod · 0.95
cumsumMethod · 0.45
RandomStateMethod · 0.45
emptyMethod · 0.45

Tested by

no test coverage detected