For generating random numbers, I've used the formula:
(int)(Math.random() * max) + min
The formula I find on Google always seem to be:
(int)(Math.random() * (max - min) + min)
Which one's right? As far as I know, I've never gotten a number that was out of my range with my formula
random() method returns a pseudorandom double type number greater than or equal to 0.0 and less than 1.0. . When this method is first called, it creates a single new pseudorandom-number generator, exactly as if by the expression new java. util.
Your formula generates numbers between min and min + max.
The one Google found generates numbers between min and max.
Google wins!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With