2

So lets say I want to generate a random number between -1000 and 1000 and I want the average to be x. How would I do this?

Edit: just to be clear, the numbers generated should fall in a standard normal distribution with an average of x.

So if I generated a thousand numbers and found the average of them it would be x.

I tried this but it doesn't seem to work:

sum_ = 0
for i in range(0, 10):
  sum_ += random.triangular(-1000, 1000, 10)
print sum_ / 10

and I want this to give me something ~10 but I'm obviously not using the right code.

8
  • random int or random float? Dec 26, 2013 at 20:40
  • You have tried anything? Dec 26, 2013 at 20:41
  • I am new to coding, I tried random.triangular, but it doesn't seem to be what I want. And float or int doesn't matter at this point. Dec 26, 2013 at 20:43
  • 1
    You must include in your post the code you have tried, and indicate where is your problem. Since you are a new user, read the StackOverflow FAQ. Dec 26, 2013 at 20:45
  • 7
    You seem to be using terms without knowing what they mean. A normal distribution always has infinite range, and the standard normal distribution is the normal distribution whose mean is zero and standard deviation is one. So it's impossible to construct "a" standard normal distribution with your criteria.
    – ruakh
    Dec 26, 2013 at 20:47

3 Answers 3

6

The standard normal distribution has infinite range, there is a non-null probability of finding points outside any given interval. You could use triangular, just remember that the mean is (a + b + mode) / 3 so triangular(a, b, 3*x - a - b) will get you what you want:

from random import triangular

a = 0
b = 10
x = 3
test = [triangular(a, b, 3*x - a - b) for _ in range(1000)]
sum(test) / 1000.0
# 3.006828109140065
2
  • Remember this doesn't satisfies normal distribution requirement. Dec 26, 2013 at 21:11
  • 1
    @Christian, the OP added the normal distribution requirement in an edit. Because this requirement is incompatible with his first limit requirement I just left my answer unmodified and added a note.
    – elyase
    Dec 26, 2013 at 21:13
4

As several comments have mentioned, these two requirements conflict with each other:

I want to generate a random number between -1000 and 1000

and

the numbers generated should fall in a standard normal distribution with an average of x

because a standard normal distribution has an infinite domain. If you choose numbers from a normal distribution, there will be some probability that you get a value greater than 1000 or less than -1000. Conversely, if you do anything to limit the range to [-1000,1000], then you will not be drawing from a normal distribution.

One option is to generate numbers according to a truncated normal distribution, which is just like a standard normal distribution except that the probability is set to zero outside the range [-1000,1000]. The easiest way to do this is to pick a number according to a normal distribution, and if it's outside the desired range, just pick again.

SIGMA=10.0 # you can pick this value to be pretty much anything
def generate_number(average):
    x = random.normal_variate(average, SIGMA)
    while x > 1000 or x < -1000:
        x = random.normalvariate(average, SIGMA)
    return x

Here SIGMA is the standard deviation of the normal distribution, which governs how spread out the values will be. If SIGMA is small and average is not close to 1000 or -1000, or to be precise: if (1000-average)/SIGMA and (1000+average)/SIGMA are both larger than 2 or 3, then this method will be fairly efficient because it will usually hit a number within the desired range [-1000,1000] the first time. But if one of those ratios is small, like around 1 or less, then the algorithm will sometimes have to loop once or twice. That probably won't be a big deal. (If you wanted to avoid it there are advanced techniques you could use, but I don't think it'd be worth the complexity.)

Another option, which is kind of what your example code in the question does, is to drop the requirement of using a normal distribution entirely, and use some other probability distribution which is naturally restricted to a certain range. Your example code, equivalent to

random.triangular(-1000,1000,mode)

uses a distribution in which the probability increases linearly from -1000 to the mode and then decreases linearly from mode to 1000. The catch with this, though, is that mode is the value which has the largest probability of being chosen. It's not the same as the average of the numbers chosen. The actual average is (min+max+mode)/3., or in your case, since min+max = 1000-1000 = 0, just mode/3, so if you wanted to generate numbers with a specified average, you would have to use

def generate_number(average):
    mode = 3*average
    if mode  < -1000 or mode > 1000:
        raise ValueError('Average cannot be satisfied: %f' % average)
    return random.normal_variate(-1000, 1000, mode)

Note that using this distribution means you can never produce numbers with an average less than -1000./3. or greater than 1000./3., unless you also adjust the min or max values accordingly.

2
    normalvariate(self, mu, sigma) method of Random instance
    Normal distribution.

    mu is the mean, and sigma is the standard deviation.

i.e.

import random
x= random.normalvariate(2,17)

Here 2 is the mean, and 17 is the standard deviation. If you want to scale linearly you would add and multiply by the appropriate values.

7
  • 2
    Why is this an accepted answer??? Dec 26, 2013 at 20:53
  • @Jack_of_All_Trades because this is the answer. It solves both the average and distribution issue in two lines of code Dec 26, 2013 at 20:55
  • 3
    No it don't! How can he provide the value for standard deviation when OP wants the number between -1000 and 1000? Dec 26, 2013 at 20:58
  • 1
    This doesn't handle the -1000 1000 requirement. Dec 26, 2013 at 20:58
  • 4
    Technically, it's impossible to satisfy the limits requirement and the normal distribution requirement.
    – David Z
    Dec 26, 2013 at 21:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.