Generate a large list of points with no duplicates

前端 未结 4 1270
悲&欢浪女
悲&欢浪女 2020-12-21 21:49

I want to create a large list containing 20,000 points in the form of:

[[x, y], [x, y], [x, y]]

where x and y can be any random integer bet

4条回答
  •  情书的邮戳
    2020-12-21 22:51

    Since n = 1001 is relatively small in your case, random.sample(population, k) will do just fine, taking a random sample of 20000 pairs from the space of possible pairs (no duplicates):

    import random
    print random.sample([[x, y] for x in xrange(1001) for y in xrange(1001)], 20000)
    

    This is the most concise and readable solution. (But if n is very big, generating the entire space of points will not be computationally efficient.)

提交回复
热议问题