MCPcopy
hub / github.com/dask/dask / pseudorandom

Function pseudorandom

dask/utils.py:515–539  ·  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

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

Used in the wild real call sites across dependent graphs

searching dependent graphs…