mardi 16 mars 2021

How to reuse a single random() instance in python

On the jvm (scala, java, kotlin, jruby etc) the following results in a single variable that is consulted repeatedly for new random values:

IntStream.range(0,10).forEach( __ -> System.out.println(rand.nextFloat()));

Is there an equivalent to next[Float|Double|Int] in python? If not what happens under the hood when invoking

import random
for i in range(10):
   print(str(random.random()))

Is the random library itself holding on to a single random reference? If not then how to ensure we avoid duplicates say by running many iterations within a few microseconds of each other? I assume the system clock gets involved in the random number generator?




Aucun commentaire:

Enregistrer un commentaire