title: Generate random points in 3D space with minimum nearest-neighbor distance
body: <p>Similar questions:<br>
  <a href="https://stackoverflow.com/questions/32810071/generating-n-random-points-with-certain-predefined-distance-between-them">Generating N random points with certain predefined distance between them</a>    </p>
  
  <p><a href="https://stackoverflow.com/questions/22152482/choose-n-most-distant-points-in-r">choose n most distant points in R</a>  </p>
  
  <p>But they are either in matlab or does not fullfill the required task.</p>
  
  <p>I have to create N number of points inside a box of length that
  the distance between any two points is larger than delta.</p>
  
  <p>For example:
  Let's say I have a box of length 10 Angstrom on x,y,z axis.<br>
  I want to have 200 random points inside this box so that minimum distance
  between any two points is larger than 3 Angstrom.</p>
  
  <p>Attempt:</p>
  
  <pre><code>#!python
  # -*- coding: utf-8 -*-#
  import numpy as np
  np.random.seed(100)
  np.set_printoptions(2)
  
  box_length = 10
  d = box_length
  threshold = 6
  num_points = 5
  x1, y1, z1 = np.random.random(num_points)* box_length, np.random.random(num_points)* box_length, np.random.random(num_points)* box_length
  x2, y2, z2 = np.random.random(num_points)* box_length, np.random.random(num_points)* box_length, np.random.random(num_points)* box_length
  
  # print(len(x1))
  
  # just for checking make ponts integers
  for i in range(len(x1)):
      x1[i] = int(x1[i])
      x2[i] = int(x2[i])
      y1[i] = int(y1[i])
      y2[i] = int(y2[i])
      z1[i] = int(z1[i])
      z2[i] = int(z2[i])
  
  
  print(x1)
  print(y1)
  print(z1)
  print("\n")
  
  pt1_lst = []
  pt2_lst = []
  for i in range(len(x1)):
      a, b, c    = x1[i], y1[i], z1[i]
      a2, b2, c2 = x2[i], y2[i], z2[i]
      dist2      = (a-a2)**2 + (b-b2)**2 + (c-c2)**2
  
      print("\n")
      print(a,b,c)
      print(a2,b2,c2)
      print(dist2)
  
      if dist2 &gt; threshold**2:
          pt1 = (a,b,c)
          pt2 = (a2,b2,c2)
          pt1_lst.append(pt1)
          pt2_lst.append(pt2)
  
  
  
  print("points")
  print(pt1_lst)
  print(pt2_lst)
  </code></pre>
  
  <p><strong>Problem on Code</strong>:
  This code compares points from points1 to points2 but does not compare within itself inside points1 and points2.</p>
  
  <p>There might be better algorithms to solve this problem and hats off to those
  guys who come with the brilliant idea to solve the problem.</p>
  
  <p>Thanks.</p>
  
  <p><strong>PS</strong>:
  I did some research and try to find related links, however was unable to solve 
  the problem.
  Still some related links are:</p>
  
  <p><strong>Update</strong>::</p>
  
  <p>I attempted Stefans' code below, It works for N= 10 but I tried it for N = 200 and it is using extremely large time ( I stopped the code after 10 minutes).</p>
  
  <p>Is there any efficient way of doing this?</p>
  
  <p>Help will be truly appreciated!!</p>
  
  <p><a href="https://stackoverflow.com/questions/11809856/all-paths-of-length-l-from-node-n-using-python">All paths of length L from node n using python</a><br>
  <a href="https://stackoverflow.com/questions/26317291/create-random-points-inside-defined-rectangle-with-python">Create Random Points Inside Defined Rectangle with Python</a>  </p>