random numbers on the half open interval
Show older comments
The function rand generates random numbers on the open interval (0,1). How can I generate random numbers on (0,1]?
Accepted Answer
More Answers (2)
Image Analyst
on 28 Aug 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 Comments
Jan
on 28 Aug 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
on 29 Aug 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
on 30 Aug 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
on 28 Aug 2013
Edited: Azzi Abdelmalek
on 28 Aug 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)
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!