numpy/randint-single-value
| Rule | randint-single-value |
|---|---|
| Pack | NumPy |
| Severity | error |
| Fix | unsafe, with xpd check --fix --unsafe-fixes |
| Library versions | every version |
The upper bound of randint is excluded, so randint(0, 1) always returns 0. Raise the upper bound by one.
coin = rng.integers(0, 1, size=10)coin = rng.integers(0, 2, size=10)What the rule reports
Section titled “What the rule reports”The rule reports np.random.randint(0, 1, n), np.random.randint(1) and rng.integers(0, 1). Each call always returns 0, because the upper bound is excluded.
How to fix it
Section titled “How to fix it”xpd check --fix --unsafe-fixes applies the fixes of this rule. Check the result, because an unsafe fix can change what the code does.
The unsafe fix writes np.random.randint(0, 2) and rng.integers(0, 2), with the other arguments kept, so the result is a 0 or a 1 where it was always 0.
Message
Section titled “Message”xpd prints this message with the issue. A name that starts with $ stands for code from your file.
The high end of randint is exclusive, so this always returns 0. Write $NP.random.randint($NEW) for a 0 or a 1.Browse the other rules of the NumPy pack.