Port Twig's "random" function