.pick_random() returns only fails to return anything if the array is empty - which means that at line 83 possible_connections is an empty array. The cause is not instantly evident from a glance at your code, so I suggest debugging the for loops to see why nothing is being appended.