how to generate random number between 10^-6 to 10^-50

i use A + (B-A)*rand(1,N) this but it doesn't help me in my matter...every time it generate number near to the minimum value it not goes to 10^-40.

 Réponse acceptée

John D'Errico
John D'Errico le 29 Mar 2018
Modifié(e) : John D'Errico le 29 Mar 2018
RAND produces a UNIFORMLY distributed random number.
Do you know what the probability that a uniformly distributed random number, when selected from the interval [1e-40,1e-6], will actually lie in the sub-interval [1e-40,1e-39]?
Roughly...
(1e-39 - 1e-40)/(1e-6 - 1e-40)
ans =
9e-34
I said roughly because MATLAB cannot actually compute the difference (1e-6-1e-40) completely accurately in double precision, but to compute the number exactly would require more effort than it is worth given the tiny difference.
So seriously, what would you expect? If you were to compute many billions of such numbers, you would still expect never to see such a result. Worse, it would never actually happen anyway, since that range of numbers is wider than the range you can compute with doubles.
Instead, you might decide to compute numbers randomly and uniformly in the interval [-40,-6]. Then raise 10 to that power. The result will NOT be uniform of course. It will have the properties that you seem to want however.

12 commentaires

Guillaume
Guillaume le 29 Mar 2018
Modifié(e) : Guillaume le 29 Mar 2018
Yes, you need to learn more about floating points and their limitations.
In particular, you have to be aware that with floating point numbers, (1e-6 - 1e-50) is exactly equal to 1e-6, as 1e-50 is insignificant compared to that magnitude.
Your formula can produce numbers in the range [1e-50 1e-40] but as John's pointed out the likelyhood of that is so tiny that you're probably never going to see one.
Two references on floating point:
i know the floating point concept thats what iam facing problem. i only want a code that my value doesn't go out the given range. it gives value like 7.23*10^-7,8.58*10^-32.....it's all random.
Guillaume
Guillaume le 29 Mar 2018
Modifié(e) : Guillaume le 29 Mar 2018
If you use
A = 1e-50;
B = 1e-6;
numbers = A + (B-A)*rand(1,N);
%or, since B >> A, this is the same:
numbers = A + B*rand(1,N);
%most of the time, the above is the same as
numbers = B*rand(1,N);
%very rarely (P < 1e-33) would the A make a difference
Then, you will only get numbers in the range [A, B]. Of course, since the distribution is uniform, the probability of them being anywhere in the range [1e-50, 1e-40] is so ridiculously small that you'll never see one.
If you're aware of the floating point concepts, then you're of course aware that numbers that span a range of that magnitude can't be added together without massive loss of precision. That is even if you did get a number C near 1e-50, there's no point adding it to a number D near 1e-6 since the result will be exactly D.
that's the problem iam facing i need only that one number should be like this 7.23*10^-7 and other number like 8.58*10^-32....that's it....nothing else..this code doesn't solve my problem...thanx for reply
The A + (B-A)*rand(1,N) calculation will not go out of range. However, it will generate uniform random, and on the scale of 1e-50 to 1e-6, most of the values are near 1e-6.
There is a random number generator which can generate all of the possible values between 0 and 1; you could multiply that by 10^-6 and add 1e-50. Half of the results would be below 1E-28 if you were to use that.
Should the distribution be uniform? If not, then John told you how to generate such numbers.
But as I keep saying, you most likely won't be able to do anything useful with numbers that span that range. For example, you can add 1 to 7.23e-7 with no issues but adding 1 to 8.58e-32 is pointless since the result is exactly 1.
>> 1+7.23e-7
ans =
1.000000723
>> 1+8.58e-32
ans =
1
Maybe the OP means to choose the exponents equally likely between -50 and -6 :-)
Maybe, and John explained how to do that in his last paragraph.
it doesn't change the fact that a number near 1e-6 can't be added or subtracted to a number near 1e-50 (without the number near 1e-50 having the same effect as 0), so what you can do with numbers that span that range is fairly limited.
Torsten is right
Torsten is right
Then, as John explained in his last paragraph:
exponent = 6 + 44*rand(1, 30);
numbers = 10.^-exponent
Of course, the distribution will not be uniform at all.
And of course, adding 1 to numbers will cancel any number smaller than about 2e-16 ( eps(1))
thanx
I fell asleep and write -40 instead of -50. Same thing, but even worse. Regardless, the answer is to work in the form of powers of 10.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Descriptive Statistics and Insights dans Centre d'aide et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by