lundi 31 juillet 2023

how to generate more fine-grained floats than normally generated by Python random()

I have the need to generate millions of random uniformly distributed numbers.

I tried the clever recipe (FullRandom) in https://docs.python.org/3/library/random.html to generate higher precision (or more fine-grained) random numbers – but it was relatively slow. If I use my GPU I can generate random numbers about 45x faster using cupy.random.random – but they aren’t as fine grained. (I can’t convert FullRandom to run on the GPU because there is no cupy equivalent of getrandbits() – I tinkered around a bit with no success.)

So I thought this may be a reasonable hack to possibly get some finer grain size in the cupy floats. Call FullRandom once, then add this to an array generated by cupy.random.random, and then normalise.

for i in range(large_integer):
    fine_grain = FullRandom.random()
    agpu = cupy.random.random(another_large_integer, dtype='float64')
    agpu += fine_grain
    agpu = agpu % 1.0000000000
    do stuff .......

QUESTION: Am I wasting my time? Does the addition and modulo just round the numbers down so that I am left with the same ‘grain size’ as the floats generated by cupy.random.random()?

Thanks in advance for your assistance.




Aucun commentaire:

Enregistrer un commentaire