Reputation: 396
double num = min + Math.random() * (max - min);
This creates random double between [min, max)
. I would like to create random double between (min, max]
.
I have seen an approach that generates integers between [min, max]
, but the number here is integer and the starting range is inclusive.
Upvotes: 0
Views: 534
Reputation: 79808
double num = max - Math.random() * (max - min);
This will produce numbers whose upper limit, max
is inclusive, but whose lower limit, min
is exclusive, because max
is returned when Math.random()
returns zero.
Upvotes: 3