Skip to content

Commit

Permalink
Use modern RNG
Browse files Browse the repository at this point in the history
  • Loading branch information
flying-sheep committed Apr 30, 2024
1 parent 0d4554b commit 8572ecb
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions scanpy/_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import numpy as np
from anndata import AnnData
from anndata import __version__ as anndata_version
from numpy.random import MT19937
from numpy.random import Generator as NPGenerator
from numpy.typing import NDArray
from packaging import version
from scipy import sparse
Expand Down Expand Up @@ -68,11 +70,19 @@ class RNGIgraph:
See :func:`igraph.set_random_number_generator` for the requirements.
"""

def __init__(self, random_state: int = 0) -> None:
self._rng = check_random_state(random_state)
def __init__(self, random_state: AnyRandom = 0) -> None:
mt = MT19937()
mt.state = check_random_state(random_state).get_state()
self._rng = NPGenerator(mt)

def __getattr__(self, attr: str):
return getattr(self._rng, "normal" if attr == "gauss" else attr)
self.gauss = self._rng.normal
self.random = self._rng.uniform
self.randint = self._rng.integers

def getrandbits(self, k: int) -> int:
numbytes = (k + 7) // 8 # bits / 8 and rounded up
x = int.from_bytes(self._rng.bytes(numbytes), "big")
return x >> (numbytes * 8 - k) # trim excess bits


def ensure_igraph() -> None:
Expand Down

0 comments on commit 8572ecb

Please sign in to comment.