How do I create a random number from a range that includes zero?
Mostra commenti meno recenti
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 Commenti
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
il 15 Ago 2018
@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
il 15 Ago 2018
@Stephen & Jan: Understood! Thanks for the great explanation
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Random Number Generation in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!