random numbers on the half open interval
Afficher commentaires plus anciens
The function rand generates random numbers on the open interval (0,1). How can I generate random numbers on (0,1]?
Réponse acceptée
Plus de réponses (2)
Image Analyst
le 28 Août 2013
0 votes
I think the same way. I mean, what is the possibility that you'll ever get exactly 1.0000000000000000000000000000000000000 anyway? Virtually never.
3 commentaires
Jan
le 28 Août 2013
The chance is small, but not 0. It is more likely, that a program fails by forgetting the 0.0 if the RNG replies 0<x<=1, but even the 1.0 might be important for any reason.
Image Analyst
le 29 Août 2013
Theoretically, but in practice you'd need to calculate a billion numbers a second for about 20 trillion trillion universe lifetimes to get a 1 if the chance was 1 in 2^52.
c=1e52 % chance to hit exactly 1.0
randNumsPerSecond = 1e9 % a billion numbers per second
secondsToGenerate = c/1e9
secondsPerYear = 60*60*24*365.24
YearsToGenerateAllNumbers = secondsToGenerate / secondsPerYear
universeLifetimes = YearsToGenerateAllNumbers / 15e9
c =
1e+52
randNumsPerSecond =
1000000000
secondsToGenerate =
1e+43
secondsPerYear =
31556736
YearsToGenerateAllNumbers =
3.16889554103441e+35
universeLifetimes =
2.11259702735627e+25
So that's why I say it doesn't matter, except in a theoretical sense.
Jan
le 30 Août 2013
Perhaps the OP was asked to write a function with these specifications and the reliability of the code must be proved by forcing the RNG to reply the extremal values. Then it is not a question of fair probability.
Btw, pi*1e7 is a nice approximation for the number of seconds per year. This is a direct effect of the almost circular trajectory of the earth ;-)
Azzi Abdelmalek
le 28 Août 2013
Modifié(e) : Azzi Abdelmalek
le 28 Août 2013
tol=0.001;
a=randi(1/tol,2000,1)*tol; %generate 2000 random numbers (0 1]
% check values equal to 1
find(a==1)
Catégories
En savoir plus sur Creating and Concatenating Matrices 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!