MCPcopy
hub / github.com/networkx/networkx / discrete_sequence

Function discrete_sequence

networkx/utils/random_sequence.py:142–170  ·  view source on GitHub ↗

Return sample sequence of length n from a given discrete distribution or discrete cumulative distribution. One of the following must be specified. distribution = histogram of values, will be normalized cdistribution = normalized discrete cumulative distribution

(n, distribution=None, cdistribution=None, seed=None)

Source from the content-addressed store, hash-verified

140
141@py_random_state(3)
142def discrete_sequence(n, distribution=None, cdistribution=None, seed=None):
143 """
144 Return sample sequence of length n from a given discrete distribution
145 or discrete cumulative distribution.
146
147 One of the following must be specified.
148
149 distribution = histogram of values, will be normalized
150
151 cdistribution = normalized discrete cumulative distribution
152
153 """
154 import bisect
155
156 if cdistribution is not None:
157 cdf = cdistribution
158 elif distribution is not None:
159 cdf = cumulative_distribution(distribution)
160 else:
161 raise nx.NetworkXError(
162 "discrete_sequence: distribution or cdistribution missing"
163 )
164
165 # get a uniform random number
166 inputseq = [seed.random() for i in range(n)]
167
168 # choose from CDF
169 seq = [bisect.bisect_left(cdf, s) - 1 for s in inputseq]
170 return seq
171
172
173@py_random_state(2)

Callers 7

gn_graphFunction · 0.90
random_referenceFunction · 0.90
lattice_referenceFunction · 0.90
directed_edge_swapFunction · 0.85
double_edge_swapFunction · 0.85

Calls 2

cumulative_distributionFunction · 0.85
randomMethod · 0.45

Tested by 1

Used in the wild real call sites across dependent graphs

searching dependent graphs…