dimanche 1 mai 2022

numpy multiple random choice

I want to generate multiple random choices without replacement from a range with numpy.

For example - I want that my output will be a 2D array, with x lines, where each line has exactly y randomly chosen numbers from the given range.

numpy.random.Generator.choice function can choose values, but you can't use the size parameter for each line separately (it represents the total number of choices)

I can call choice in a loop, but I'm looking for something more efficient.

My current code:

rand = numpy.random.default_rng()
output = numpy.zeros((x,y))

for i in range(x):
    output[i, :] = rand.choice(x, y, replace=False, shuffle=False)



Aucun commentaire:

Enregistrer un commentaire