Cache random function for less overhead