Click to See Complete Forum and Search --> : Random with Min, Max


code?
August 19th, 2008, 10:59 PM
How would I generate an integer that is between -1 and 1 with Math.random()?

javajawa
August 20th, 2008, 02:47 AM
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

code?
August 20th, 2008, 03:02 AM
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.


var random = Math.random() * -2 + 1;


Works well.

javajawa
August 20th, 2008, 03:03 AM
That's exactly the same, you realise. Just replace min with 1 and max with -1 :D