0
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.

swamp
  • 276
  • 1
  • 8

1 Answers1

3
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.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110