Random placement of cylinders with random orientations
I have this script, 

...    
# Function to calculate the distance between two points
def distance(a, b) :
    '''
    list , list --&gt; float
    a = list with three element
    b = list with three element

    return the distance between two points in float
    '''
    return sqrt((a[0]-b[0])**2+(a[1]-b[1])**2+(a[2]-b[2])**2)

# How many Sphere you want to add
count = 50

# Cylinder properties
radius = 0.03
depth = 0.2

# The sphere will be created between -domain &lt;--&gt; domain
domain = 0.5

# Max time to try finding a new location before break the loop
maxTry = 50

# Variable to count how many try has been done
Try = 0

# list with three element to generate locations
location = [0, 0, 0]

# Variable to hold the distant between balls
dist = 0

# True == Won't collide , False == Will collide
State = True

# This tuple will hold centers of created spheres
locList = ()

while count &gt; 0 :

    # Calculate x, y, z position
    location[0] = randint(-domain*10, domain*10)/10.0
    location[1] = randint(-domain*10, domain*10)/10.0
    location[2] = randint(-domain*10, domain*10)/10.0

    # Start check if it will collide with other spheres
    for x in locList :
        # Calculate the distant
        dist = distance(x, location)

        # If it's too close make State = False
        if dist &lt; radius*2 + 0.1 :
            State = False
            break
        # If it's in a good position State = True
        else :
            State = True

    # The distant is too close , recalculate the location
    if State == False :
        Try += 1
        continue

    # After Try reach maxTry break the loop
    if Try &gt; maxTry :
        break

    # Successfully found a New location
    # Add this point
    locList += (location[:],)

    # Smooth the faces
    bpy.ops.object.shade_smooth()

    #Create a new sphere at the same location, and resize it to an cylinder
    bpy.ops.mesh.primitive_cylinder_add(radius = 0.1, location = location)

    #Give the cylinder a random orientation
    bpy.ops.transform.rotate(value=(2*pi*random()-1), axis=((2*pi*random()-1), (2*pi*random()-1), (2*pi*random()-1)))

    #Smooth the faces
    bpy.ops.object.shade_smooth()

    # reset Try
    Try = 0

    # Decrease the counter
    count -= 1     


The problem of this script is in the condition of overlap between two cylinder, someone can solve this problem of overlap?