PythonRandomInterface is included for backward compatibility New code should use PythonRandomViaNumpyBits instead.
| 357 | |
| 358 | ################################################################## |
| 359 | class PythonRandomInterface: |
| 360 | """PythonRandomInterface is included for backward compatibility |
| 361 | New code should use PythonRandomViaNumpyBits instead. |
| 362 | """ |
| 363 | |
| 364 | def __init__(self, rng=None): |
| 365 | try: |
| 366 | import numpy as np |
| 367 | except ImportError: |
| 368 | msg = "numpy not found, only random.random available." |
| 369 | warnings.warn(msg, ImportWarning) |
| 370 | |
| 371 | if rng is None: |
| 372 | self._rng = np.random.mtrand._rand |
| 373 | else: |
| 374 | self._rng = rng |
| 375 | |
| 376 | def random(self): |
| 377 | return self._rng.random() |
| 378 | |
| 379 | def uniform(self, a, b): |
| 380 | return a + (b - a) * self._rng.random() |
| 381 | |
| 382 | def randrange(self, a, b=None): |
| 383 | import numpy as np |
| 384 | |
| 385 | if b is None: |
| 386 | a, b = 0, a |
| 387 | if b > 9223372036854775807: # from np.iinfo(np.int64).max |
| 388 | tmp_rng = PythonRandomViaNumpyBits(self._rng) |
| 389 | return tmp_rng.randrange(a, b) |
| 390 | |
| 391 | if isinstance(self._rng, np.random.Generator): |
| 392 | return self._rng.integers(a, b) |
| 393 | return self._rng.randint(a, b) |
| 394 | |
| 395 | # NOTE: the numpy implementations of `choice` don't support strings, so |
| 396 | # this cannot be replaced with self._rng.choice |
| 397 | def choice(self, seq): |
| 398 | import numpy as np |
| 399 | |
| 400 | if isinstance(self._rng, np.random.Generator): |
| 401 | idx = self._rng.integers(0, len(seq)) |
| 402 | else: |
| 403 | idx = self._rng.randint(0, len(seq)) |
| 404 | return seq[idx] |
| 405 | |
| 406 | def gauss(self, mu, sigma): |
| 407 | return self._rng.normal(mu, sigma) |
| 408 | |
| 409 | def shuffle(self, seq): |
| 410 | return self._rng.shuffle(seq) |
| 411 | |
| 412 | # Some methods don't match API for numpy RandomState. |
| 413 | # Commented out versions are not used by NetworkX |
| 414 | |
| 415 | def sample(self, seq, k): |
| 416 | return self._rng.choice(list(seq), size=(k,), replace=False) |
no outgoing calls
searching dependent graphs…