How do I create a random number from a range that includes zero?

I'm looking for a way to create uniformly distributed random numbers between 0 and 1 (including 0, excluding 1). The Matlab function rand() excludes zero. It is using the range (0, 1).
In Python/Numpy the functions random.random() is using the range [0.0, 1.0). That is why I hope it is mathematically/informatically possible to implement such random numbers.
My first thought was to subtract the default minimum value of rand() so that zero is actually possible. However, I couldn't find such a minimum value.
Any ideas? Many thanks in advance!

댓글 수: 5

jonas
jonas 2018년 8월 15일
편집: jonas 2018년 8월 15일
I didn't know that rand excludes zero, but you could use something like this:
randi([0 2^52])/2^52
Stephen23
Stephen23 2018년 8월 15일
편집: Stephen23 2018년 8월 15일
"... randn() or normrnd() exclude zero. They are using the range (0, 1)."
Really? How could a distribution over the range (0,1) could be called normal?
@jonas: the rand documentation states "returns a single uniformly distributed random number in the interval (0,1)", so it clearly excludes zero and one. You should put your comment as an answer.
You are correct. For the normal ditributed numbers the zero is included. I will edit the question so that it only refers to rand(). the MATLAB-documentation of "rand()" at least says: "returns a single uniformly distributed random number in the interval (0,1)."
Jan
Jan 2018년 8월 15일
편집: Jan 2018년 8월 15일
@Jonas: randi([0 2^52])/2^52 is not equally distributed in the standard sense of random data with 53 used bits. The difference is tiny, but existing. But a small change will fix this:
randi([0, 2^52-1]) / 2^52
@Stephen & Jan: Understood! Thanks for the great explanation

댓글을 달려면 로그인하십시오.

 채택된 답변

Jan
Jan 2018년 8월 15일
편집: Jan 2020년 12월 15일
For a double with 32 bit used random bits and [0, 1):
r = randi([0, 4294967295]) / 4294967296.0 % constants: 2^32-1 and 2^32
[TYPO FIXED: "2^31-1" ==> "2^32-1"]
With 53 bit precision (as by rand()) and [0, 1):
a = randi([0, 4294967295]) * 32; % Constant: 2^32-1
b = randi([0, 4294967295]) * 64;
r = (a * 67108864.0 + b) / 9007199254740992.0; % Constants: 2^26, 2^53
For 53 bit precision (as by rand()) and [0, 1]:
r = (a * 67108864.0 + b) / 9007199254740991.0; % Constants: 2^26, 2^53-1
I assume that this is not efficient, because I do not know the method to calculate randi([0, 4294967295]). Creating 32 random bits is cheap, but the limitation to a certain interval is expensive, because the modulo operation would be flawed. In the case of [0, 2^32-1] there is no need to limit the interval, because the 32 random bits can be used directly.
I'm planning to publish a Mersenne-Twister MEX function, which provides the [0,1) and [0,1] intervals directly.

댓글 수: 3

+1 excellent answer
In your script for a double with 32 bit, you wrote the constants 2^31-1 and 2^32. But I guess what you meant is 2^31-1 and 2^31. And the numbers are 2147483647 and 2147483648. Then, the script must be modified as
r = randi([0, 2147483647]) / 2147483648 % constants: 2^31-1 and 2^31
Am I right?
@Keonwook Kang: Thanks for finding this typo. Actually only the comment was wrong and the numbers are "2^32-1 and 2^32".
randi([0, 4294967295]) produces numbers between 0 and (binary) 11111111 11111111 11111111 11111111 (32 bits). This means, that 2^32-1 is the correct upper limit and dividing it by 2^32 results in an output of [0, 1).

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Random Number Generation에 대해 자세히 알아보기

질문:

2018년 8월 15일

댓글:

Jan
2020년 12월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by