There are 3 options I can think of:
- you can pre-generate non-redundant coordinates to a suitable precision and use the
sample
function from the random
built-in library
- you can save the coordinates to a list and do
if coord in coord_list
conditional before adding to the list
- you can use a
set()
, which cannot contain any duplicate objects, i.e. set(1,1,2,3,4,4,5)
becomes {1,2,3,4,5}
.
Given your function though, I think the number of repetitions will be negligible unless you're generating a lot of coordinates.
Applying the most performant option (3) to your code would look something like:
import numpy as np
def get_random_angles(n):
phis = np.random.uniform(0, 2*np.pi, n)
thetas = np.arccos(1 - np.random.uniform(0, 2, n))
while l := len(set(zip(thetas,phis))) != n:
t,p=get_random_angles(n-l)
np.concatenate(thetas,t)
np.concatenate(phis,p)
return thetas, phis