For small sample sets testing collision can be done like this:
import random
import bpy
obj_ctr = []
obj_radius = 1
# one object must be created outside the loop for data structure to be available for testing
x = random.randint(-5, 4)
y = random.randint(-2, 7)
z = random.randint(3, 10)
obj_ctr.append((x,y,z))
#while len(obj_ctr) < 10:
for a in range(10):
test_x = False
test_y = False
test_z = False
x = random.randint(-5, 4)
y = random.randint(-2, 7)
z = random.randint(3, 10)
for test in obj_ctr: #verify the new randoms will not allow collision
if abs(test[0] - x) < obj_radius * 2:
test_x = False
else:
test_x = True
if abs(test[1] - y) < obj_radius * 2:
test_y = False
else:
test_y = True
if abs(test[2] - y) < obj_radius * 2:
test_z = False
else:
test_z = True
if (test_x and test_y and test_z):
obj_ctr.append((x,y,z))
for obj in obj_ctr:
bpy.ops.mesh.primitive_cube_add(radius = obj_radius, location = obj)
As you attempt to create more items iterating through all the previously created items to test for intersection will become increasingly time consuming.If you use the while loop (to ensure 10 objects) be careful that the random ranges are large enough or radius is set small enough to avoid infinite loops.
Poisson Sampling
$\endgroup$