SQL function for getting random circ chains