Randomly 1 or -1

math.random(1,2) * 2 - 3

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.

Last updated