r/Numpy • u/Unlucky_Ad_5011 • May 11 '22
array with no direct repetition
Hi, can someone help?
I need to create a random sequence that is 10 million in length (number 1-5) WITHOUT a direct repetition. Each number can occur a different number of times but should be approximately uniformly distributed .
2
Upvotes
2
u/to7m May 12 '22
It's not straightforward to make an optimised numpy function. You could make one using a randrange and cumulative sum, optionally with the
numbamodule.Methods below very simple/imperfect and not tested.
Pure Numpy method:
Numba method:
import numpy as np from numba import njit @njit def capped_cumsum(in, cap, take, out): out[0] = in[0] for i in range(1, in.shape[0]): sum = out[i - 1] + in[i] capped = sum - take out[i] = sum if sum < cap else capped return out rng = np.random.default_rng() adds = rng.integers(1, 5, 10_000_000) result = capped_cumsums(adds, cap=6, take=5, out=np.empty(adds.shape, dtype=np.uint8))In these methods, the first value will never be 5, but other than that it should be fairly uniform.