from random import random


def triangle_random_points(num_points, loop_triangles):
    """
    Generates a list of random points over mesh loop triangles.

    :arg num_points: the number of random points to generate on each triangle.
    :type int:
    :arg loop_triangles: list of the triangles to generate points on.
    :type loop_triangles: :class:`bpy.types.MeshLoopTriangle`, sequence
    :return: list of random points over all triangles.
    :rtype: list
    """


    # For each triangle, generate the required number of random points
    sampled_points = [None] * (num_points * len(loop_triangles))
    for i, lt in enumerate(loop_triangles):
        # Get triangle vertex coordinates
        verts = lt.id_data.vertices
        ltv = lt.vertices[:]
        tv = (verts[ltv[0]].co, verts[ltv[1]].co, verts[ltv[2]].co)

        for k in range(num_points):
            u1 = random()
            u2 = random()
            u_tot = u1 + u2

            if u_tot > 1:
                u1 = 1.0 - u1
                u2 = 1.0 - u2

            side1 = tv[1] - tv[0]
            side2 = tv[2] - tv[0]

            p = tv[0] + u1 * side1 + u2 * side2

            sampled_points[num_points * i + k] = p

    return sampled_points