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 件のコメント
I didn't know that rand excludes zero, but you could use something like this:
randi([0 2^52])/2^52
"... 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.
JanPK
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
jonas
2018 年 8 月 15 日
@Stephen & Jan: Understood! Thanks for the great explanation
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Random Number Generation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!