random numbers on the half open interval

10 views (last 30 days)
The function rand generates random numbers on the open interval (0,1). How can I generate random numbers on (0,1]?

Accepted Answer

Jan
Jan on 28 Aug 2013
Edited: Jan on 28 Aug 2013
The chance to get 1.0 exactly is 1:2^52. It is small, but when you want to be sure, that your code run properly, such details should not be neglected. I have personally seen a function crashing, because it did not catch the case, when the random value is exactly zero.
Although the algorithm for random numbers in [0, 1) is fast due to its simplicity, Matlab's RAND produces (0,1). I did not find an algorithm for this, so I assume, that the 0.0 is simply rejected. It is not trivial to inject it afterwards, but you can emulate the algorithm relative efficiently by obtaining two 32bit integers:
s = RandStream('mt19937ar','Seed',0)
RandStream.setGlobalStream(s);
r1 = randi([0, 4294967295]);
r2 = randi([0, 4294967295]);
r = (2097152 * r1 + fix(r2 / 2048)) / 9007199254740992;
For 0<=r<=1 you need:
r = (2097152 * r1 + fix(r2 / 2048)) / 9007199254740991;
For 0<r<=1:
r = (1 + 2097152 * r1 + fix(r2 / 2048)) / 9007199254740992;
Perhaps this is faster when UINT32 types are used in RANDI, but I cannot test this currently.
Well, I do not think that this is nice. Encapsulating this inside a C-MEX function would faster and perhaps more reliable. I'm working on such a function, because I'm really too confused by the changes in RAND, RNG and RANDSTREAM, when I write software which must be compatible with Matlab 6.5 to 2013a.
NOTE: There have been many bugs in published software for RNGs. Be sure to test this by your own before using it. Set r1 and r2 to 0 and 2^32-1 manually to check if the results really matches the claimed range!

More Answers (2)

Image Analyst
Image Analyst on 28 Aug 2013
I think the same way. I mean, what is the possibility that you'll ever get exactly 1.0000000000000000000000000000000000000 anyway? Virtually never.
  3 Comments
Image Analyst
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
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 ;-)

Sign in to comment.


Azzi Abdelmalek
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!