Skip to content

numpy/random-draw-loop

Rule random-draw-loop
Pack NumPy
Severity warning
Fix none, edit by hand
Library versions every version

Drawing from a Generator once per pass of a loop makes one call for every pass. When no pass depends on another, draw all values at once with size= and compute on the array.

errors = []
for _ in range(units):
base = rng.uniform(20, 60)
errors.append(base * grid - cost)
base = rng.uniform(20, 60, size=(units, 1))
errors = base * grid - cost

The rule reports for _ in range(n) whose body calls a method of a Generator named rng, such as rng.uniform, rng.normal or rng.integers.

xpd has no automatic fix, so make the change by hand.

The loop variable _ says that no pass depends on its position, so every pass can be drawn in one call with size=, and the arithmetic then runs on an array with one more axis. The numbers come in another order, so a result that is compared with an earlier run has to be made again. Keep the loop when a pass reads what an earlier pass wrote.

xpd prints this message with the issue. A name that starts with $ stands for code from your file.

This loop draws from $RNG once per pass. When no pass depends on another, draw every pass at once with size=, as in $RNG.$DRAW(low, high, size=(units, 1)), and compute on the whole array. The numbers then come in another order.

Browse the other rules of the NumPy pack.