Java Math.random()
returns a random number in [0..1). Is there a way to make 1.0 inclusive? Eventually I would like to have a random number (floating point number) in a specific range such as [Min..Max]. We know that for integers it will be
Min + (int)(Math.random() * ((Max - Min) + 1))
I reviewed answers shown below in "Random floating point double in Inclusive Range". Alex L. answer is very close, but it is not quite right as Kendall Prey commented.
function randomInRange(min, max) {
return Math.random() < 0.5 ? ((1-Math.random()) * (max-min) + min) : (Math.random() * (max-min) + min);
}
To use it in a Monte Carlo simulation application, two boundaries should be either inclusive or exclusive exactly.