mercredi 28 avril 2021

Generating uniform random numbers between two large integers in fortran90/95 or python

I want to generate some uniform random numbers between [2E16, 5E20] in fortran90/95 or python. I know that if "R" is a random number between zero and one (0<R<1), we can transform R to [A, B] as:

R = (B-A)*R + A.

But when I use this algorithm to generate some random numbers between [2E16, 5E20], most of generated random numbers are very close to 5E20. I know why this is occurred, it is because of that, the term B-A is approximately equal to B (because B is so bigger than A), and most of the generated random numbers are placed within the magnitude of B or B/10 (I mean it was very close to B).

How can I solve this problem?

One solution that I tried to apply is that, I split the [2E16, 5E20] interval into smaller intervals, such as:

[2E16, 2E17], [2E17, 2E18], [2E18, 2E19], [2E19, 2E20], [2E20, 5E20].

And I generated some random numbers between each of the above intervals.

Is there any other solutions? Is my solution correct?

Thanks in advance!




Aucun commentaire:

Enregistrer un commentaire