Numpy random choice of tuples Numpy random choice of tuples numpy numpy

Numpy random choice of tuples


Use choice to choose the 1dim indices into the array, then index it.

In the example you provided, only the number of possible choices affects the nature of the choice, not the actual values (0, 255). Choosing indices is the 1dim problem choice knows how to handle.

choices = numpy.array([[0,0,0],[255,255,255]])idx = numpy.random.choice(len(choices),4)choices[idx]


Just adding this answer to provide a non-numpy based answer:

choices = ((0,0,0),(255,255,255))from random import choiceprint tuple(choice(choices) for _ in range(4))


If you want to specifically sample without replacement, you can try:

import randomchoices = ((0,0,0),(1,1,1),(2,2,2),(3,3,3))random.sample(choices, 2)