Moved nested functions out
Revised random function to be consistent with its introduction in 3.3.1