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.
CodePudding user response:
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.
CodePudding user response:
If you would like you could use ThreadLocalRandom#nextDouble(double, double)
to produce the desired results.
CodePudding user response:
Try this:
double num = (min Math.random() * (max - min)) 1;