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)
| 140 | |
| 141 | @py_random_state(3) |
| 142 | def 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) |
searching dependent graphs…