Book of Defold
Search…
Randomly 1 or -1
1
math.random(1,2) * 2 - 3
Copied!
The above code can be used to get a random value of either 1 or -1.
1 * 2 = 2 - 3 = -1
2 * 2 = 4 - 3 = 1
Getting 1 or -1 can be useful for setting a value to be randomly negative or positive when multiplied by the random value. This can be useful for example for setting a direction randomly.
https://repl.it/@pkeod/Randomly1or-1
repl.it
Last modified 3yr ago
Copy link