How would I generate an integer that is between -1 and 1 with Math.random()?
Printable View
How would I generate an integer that is between -1 and 1 with Math.random()?
Code:function Random(min, max) {
return (min + (max - min) * Math.random());
};
//Logic Test: Math.random() = 0
// min + (0) = min
// Logic Test: Math.random = 1
// min + (max - min) = max
Thanks, the function is similar to what I found googling.
Anyways, I think I figured out the one-liner way to do it, which is what I wanted.
Works well.Code:var random = Math.random() * -2 + 1;
That's exactly the same, you realise. Just replace min with 1 and max with -1 :D