I need to randomly pick a number (this is in java, using Math.random()) between -N and N. Specifically, in this current case I need to pick a random number between -1 and 1. All the results I've found has explained how to find a random number between some positive numbers.
Right now I'm using this statement, which only covers half of what I need.
double i = Math.random();
For a random number between -n and n:
/**
* @return a random number, r, in the range -n <= r < n
*/
public static double getRandom(double n) {
return Math.random()*n*2 - n;
}
Just use:
2 * Math.random() - 1
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