Best way to generate random floats over a wide range of magnitudes

Simple question: rand() generally returns a random number rarely in the range (0,1E-1) and I am interested in a range of random numbers between (0,1E-2).

I was wondering if anyone could verify that a solution like

rand() * (10.)^(rand(-16:0,1)[1])

would do generate fair random numbers in this range. Or maybe there is a more elegant (or built-in) solution?

1 Like

If you want uniformly distributed numbers (as rand provides in the range 0 to 1) then you just scale:

rand() * 1e-2

If you need numbers with their log10 uniformly distributed between -16 and 0, as I guessed from the line of code, they can be generated with 10 ^ (-16*rand()).

2 Likes