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)
| 513 | |
| 514 | |
| 515 | def 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 | |
| 542 | def random_state_data(n: int, random_state=None) -> list: |
no test coverage detected
searching dependent graphs…