-2

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?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
  • 3
    Welcome, please take the [tour]. Your question is very confusing. It seems you do not actually want a uniform distribution but some logarithmic distribution. – Vladimir F Героям слава Apr 28 '21 at 14:03
  • 3
    Further to Vladimir's comment about logarithmic distributions, there simply are more integers between 1e19 and 1e20 than there are between 1e18 and 1e19. Ten times more. – francescalus Apr 28 '21 at 14:09
  • If you want every integer in the range to have an equal chance of showing up, and you're willing to use python, what's wrong with `random.randint(20000000000000000, 500000000000000000000)`? – pjs Apr 28 '21 at 23:10

1 Answers1

1

This is less of a programming problem, and more of a maths problem.

You need to think about which distribution you are drawing your random numbers from. Any uniform distribution which spans several orders of magnitude will always produce more numbers with larger exponents than with smaller exponents.

If you want a distribution which produces the same number of numbers with each exponent, you should consider the log uniform distribution, which can be sampled using e.g. scipy.stats.loguniform.

veryreverie
  • 2,871
  • 2
  • 13
  • 26