I have an unknown graph where the number of vectors are nv
and the number of arcs are na
. I want to randomly determine na
unique arcs using Python. I am struggling with the unique constraint on my problem.
I first attempted this without the unique constraint, which seemed to work:
np.random.randint(nv, size=(na,2))
If I have 3 vertices and want 5 arcs this could produce:
array([[2, 2],
[2, 1],
[1, 0],
[0, 1],
[2, 1]])
Which produces the arc [2,1]
twice. I would therefore wish for my script to be able to identify that a arc is already been randomly chosen, and then chose a new unique random arc. However, I can't seem to determine whether this is possible using randint
. Is there a different way that I should approach this?
You can generate an array with the combinations and sample unique values with np.random.choice
:
import numpy as np
N = 3
S = 5
out = np.mgrid[0:N, 0:N].reshape(2, -1).T[np.random.choice(N**2, S, replace=False)]
Output:
array([[1, 0],
[2, 0],
[2, 1],
[0, 0],
[2, 2]])