random numbers on the half open interval
显示 更早的评论
The function rand generates random numbers on the open interval (0,1). How can I generate random numbers on (0,1]?
采纳的回答
更多回答(2 个)
Image Analyst
2013-8-28
0 个投票
I think the same way. I mean, what is the possibility that you'll ever get exactly 1.0000000000000000000000000000000000000 anyway? Virtually never.
3 个评论
Jan
2013-8-28
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
2013-8-29
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
2013-8-30
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
2013-8-28
编辑:Azzi Abdelmalek
2013-8-28
tol=0.001;
a=randi(1/tol,2000,1)*tol; %generate 2000 random numbers (0 1]
% check values equal to 1
find(a==1)
类别
在 帮助中心 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!