lundi 14 novembre 2016

Random, Non-Repeating 2D List Python

I have a program that generates a two dimensional list containing a set number of 2 dimensional co-ordinates within a certain range. (It doesn't matter if the nested are lists or tuples) eg:

[[5, 0], [4, 6], [9, 7], [2, 9], [2, 6]]

The problem is that I wish for no two sublists to be equal while keeping the length of the outer list the same.

i have tried using random.sample:

pointx = random.sample(range(10),5)
pointy = random.sample(range(20),5)
points = list(zip(pointx,pointy))

where 10 is the x range, 20 is the y range and 5 is the amount of points:

[(1,0),(9,19),(8,13),(3,5),(0,14)]

However this method's maximum amount of points is only 10+20 where it clearly should be 10*20 because of the separate sample lists.

While writing this, i have realised that:

point2D = []
point1D = random.sample(range(10*20),5)
[point2D.append(divmod(i,10)) for i in point1D]

This is a viable approach, but also clumsy, i was wondering whether there was a better solve possibly involving numpy.

Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire