Random Number Ranges

How do I get a random number somewhere between -5 and +5?


Random x only works for positive numbers and starts at 1. To get a negative number, or a range of numbers that goes below 0, you need to subtract from your positive random number. In order to start your range of random numbers at a positive number greater than 1, you'll need to add to your random number.
Start by figuring out how big your range of numbers is. Take the highest number in your range and subtract the lowest number in your range. If you want a range from +5 to -5: 5 - (-5) = 10

Next we want to find a random number in that range and either lift or lower the range by our lowest number.
In TNG, when you call for a random number it starts at +1; if you asked for random 10 you have a chance of getting 1, 10 and anything in between. The main thing is to be aware of this, but if you're trying to get an equal number of positive and negative numbers, you'll have to subtract an extra one. Otherwise the number will be slightly biased towards being positive.

Submitted by talaya on 6. March 2009 - 22:07