diff --git a/xgi/generators/random.py b/xgi/generators/random.py index 91ed0284..62d5c698 100644 --- a/xgi/generators/random.py +++ b/xgi/generators/random.py @@ -11,6 +11,7 @@ from .classic import empty_hypergraph from .lattice import ring_lattice +from .uniform import uniform_erdos_renyi_hypergraph __all__ = [ "random_hypergraph", @@ -20,7 +21,7 @@ ] -def random_hypergraph(N, ps, order=None, seed=None): +def random_hypergraph(n, ps, order=None, seed=None): """Generates a random hypergraph Generate N nodes, and connect any d+1 nodes @@ -28,7 +29,7 @@ def random_hypergraph(N, ps, order=None, seed=None): Parameters ---------- - N : int + n : int Number of nodes ps : list of float List of probabilities (between 0 and 1) to create a @@ -56,19 +57,21 @@ def random_hypergraph(N, ps, order=None, seed=None): >>> H = xgi.random_hypergraph(50, [0.1, 0.01]) """ - if seed is not None: - np.random.seed(seed) + ps = np.array(ps) if order is not None: if len(ps) != 1: raise ValueError("ps must contain a single element if order is an int") - if (np.any(np.array(ps) < 0)) or (np.any(np.array(ps) > 1)): + if (ps < 0).any() or (ps > 1).any(): raise ValueError("All elements of ps must be between 0 and 1 included.") - nodes = range(N) + nodes = range(n) hyperedges = [] + H = empty_hypergraph() + H.add_nodes_from(nodes) + for i, p in enumerate(ps): if order is not None: @@ -76,17 +79,10 @@ def random_hypergraph(N, ps, order=None, seed=None): else: d = i + 1 # order, ps[0] is prob of edges (d=1) - potential_edges = combinations(nodes, d + 1) - n_comb = comb(N, d + 1, exact=True) - mask = np.random.random(size=n_comb) <= p # True if edge to keep - - edges_to_add = [e for e, val in zip(potential_edges, mask) if val] - - hyperedges += edges_to_add - - H = empty_hypergraph() - H.add_nodes_from(nodes) - H.add_edges_from(hyperedges) + size = d + 1 + if p > 0: + h_uniform = uniform_erdos_renyi_hypergraph(n, size, p, seed=seed) + H << h_uniform return H