Skip to content

Instantly share code, notes, and snippets.

@spyesx
Last active October 26, 2022 14:29
Show Gist options
  • Save spyesx/bb1742fe863c0b8a0191 to your computer and use it in GitHub Desktop.
Save spyesx/bb1742fe863c0b8a0191 to your computer and use it in GitHub Desktop.
Several methods to get a random positive or negative number
// Math.random() will give you a random decimal number between 0 and 1
// Just check if the number is greater than 0.5
// And apply -1 or 1 depending on the result
Math.random() < 0.5 ? -1 : 1;
// Math.random() will give you a random decimal number between 0 and 1
// Math.rand() of a Math.random() will give you an integer 0 or 1
// Use 0 or 1 as a result of a condition to use -1 or 1
Math.round(Math.random()) ? -1 : 1;
// /!\ NOTE : Can be used for any other values like 9 or 37 ; -2 or 2...
// Math.round(Math.random()) will give you 0 or 1
// Multiplying the result by 2 will give you 0 or 2
// And then subtracting 1 gives you -1 or 1.
Math.round(Math.random()) * 2 - 1;
// Math.random() will give you a random decimal number between 0 and 1
// Since every number less than 1 give false in a condition, we need something between 0 and 2
// Multiplying the result by 2 will give you a number between 0 and 2
// Now we could have something like 0.4 false or 1.02 or even 1.68
// Which would give in a condition false, false, true
// We can now add a condition OR to our system to return 0 or 1 because:
// false|0 == 0 but true|0 == 1 because the second condition will not be checked => | not ||
// From now we have a random number 0 or 1
// Now set an array with two values. The idea is to select one of these values via it's index.
// Select the value index 0 or 1 from the array by calling it directly (ex: [-1,1][0] == -1)
[-1,1][Math.random()*2|0];
// /!\ NOTE : Can be used for any other values like 9 or 37 ; -2 or 2...
// Math.random() will give you a random decimal number between 0 and 1
// Since every number less than 1 give FALSE in a condition, we need something between 0 and 2
// Multiplying the result by 2 will give you a number between 0 and 2
// Now we could have something like 0.4 FALSE or 1.02 or even 1.68
// Which would give in a condition FALSE, FALSE, true
// We can now add a condition OR to our system to return 0 or 1 because:
// false|0 == 0 but true|0 == 1 because the second condition will not be checked => | not ||
// From now we have a random number 0 or 1 that we can use as TRUE or FALSE in a condition.
// Lets add this condition with a || to use also the second value
Math.random()*2|0 || -1;
// Math.random() will give you a random decimal number between 0 and 1
// Math.rand() of a Math.random() will give you an integer 0 or 1
// Multiply this number by 2 and you'll have 0 or 2
// Substract 1 and you'll have -1 or 1
-1 + Math.round(Math.random()) * 2;
// Math.random() will give you a random decimal number between 0 and 1
// Math.rand() of a Math.random() will give you an integer 0 or 1
// PI * 0 = 0 and PI * 1 = PI
// cos(0) = 1
// cos(PI) = -1
// So : cos( PI x ( 0 or 1 ) ) will give you 1 or -1
Math.cos( Math.PI * Math.round( Math.random() ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment