Discussion:
Rand across orders of magnitude
(too old to reply)
Theresa
2008-05-15 19:20:18 UTC
Permalink
Hi everyone,

I am trying to generate a set of random numbers on the
interval [1e-7 1e-2]. I'm using rand and the formula given
in Matlab's help file:

x = a + (b-a) * rand(n)

where a = 1e-7 and b = 1e-2

I would like to generate random numbers that are uniformly
distributed across all orders of magnitude, but this method
generates random numbers that are concentrated near 1e-2,
with very few near 1e-7. Can anyone suggest a method for my
problem? Any help would be very much appreciated

Thanks
Theresa
Roger Stafford
2008-05-15 19:36:03 UTC
Permalink
Post by Theresa
Hi everyone,
I am trying to generate a set of random numbers on the
interval [1e-7 1e-2]. I'm using rand and the formula given
x = a + (b-a) * rand(n)
where a = 1e-7 and b = 1e-2
I would like to generate random numbers that are uniformly
distributed across all orders of magnitude, but this method
generates random numbers that are concentrated near 1e-2,
with very few near 1e-7. Can anyone suggest a method for my
problem? Any help would be very much appreciated
Thanks
Theresa
----------
How about

x = 10^(log10(a)+(log10(b/a))*rand(n,1));

Of course this is NOT a uniform distribution on [a,b], but that is apparently
not what you are asking for.

Roger Stafford
Theresa
2008-05-15 20:48:01 UTC
Permalink
Roger - thanks so much! This is exactly what I wanted to
do. And your response was so fast! This is very much
appreciated.
<g0i2di$do4
Post by Roger Stafford
Post by Theresa
Hi everyone,
I am trying to generate a set of random numbers on the
interval [1e-7 1e-2]. I'm using rand and the formula given
x = a + (b-a) * rand(n)
where a = 1e-7 and b = 1e-2
I would like to generate random numbers that are uniformly
distributed across all orders of magnitude, but this method
generates random numbers that are concentrated near 1e-2,
with very few near 1e-7. Can anyone suggest a method for my
problem? Any help would be very much appreciated
Thanks
Theresa
----------
How about
x = 10^(log10(a)+(log10(b/a))*rand(n,1));
Of course this is NOT a uniform distribution on [a,b],
but that is apparently
Post by Roger Stafford
not what you are asking for.
Roger Stafford
Roger Stafford
2008-05-16 00:56:02 UTC
Permalink
Post by Theresa
Roger - thanks so much! This is exactly what I wanted to
do. And your response was so fast! This is very much
appreciated.
-----------
I made it more complicated than necessary. It could just as well have been the
shorter but equivalent:

x = a*((b/a).^rand(n,1));

Roger Stafford

Continue reading on narkive:
Loading...