-2

l would like to generate 6 random variables between 0 and 1 [0,1] and deduce the seventh from the sum of the six values. For instance we have :

alpha1,alpha2,alpha3,alpha4,alpha5,alpha6

are generated randomly in this interval [0,1]. Now l would like to deduce alpha7 as follow :

sum(alpha1,alpha2,alpha3,alpha4,alpha5,alpha6)+ alpha7 =1

hence :

alpha7= 1-sum(alpha1,alpha2,alpha3,alpha4,alpha5,alpha6)

l did the following :

>>> r = numpy.random.random(6) 
>>> r
array([ 0.7415801 ,  0.43230563,  0.29287991,  0.41897992,  0.54627315,
        0.4071017 ])
>>> r.sum()
2.8391204236894456
alpha7=1-r.sum()
-1.8391204236894456

However it violates :

sum(alpha1,alpha2,alpha3,alpha4,alpha5,alpha6)+ alpha7 =1

because sum(alpha1,alpha2,alpha3,alpha4,alpha5,alpha6) should be less than 1 and alpha7 is in [0,1] such that

**sum(alpha1,alpha2,alpha3,alpha4,alpha5,alpha6)+ alpha7 =1**
Joseph
  • 73
  • 2
  • 9
  • Why not pick seven random numbers, add them together and scale all of them such that the total is 1? – jonrsharpe Dec 13 '17 at 13:24
  • @jonrsharpe, the purpose is to deduce the seventh value value from the the sum of the first six values – Joseph Dec 13 '17 at 13:25
  • 1
    That doesn't really seem like a purpose; that's what you're trying to do, but not why you're trying to do it. What's the actual *context*? – jonrsharpe Dec 13 '17 at 13:26
  • The context is to infer a random variable from the sum of a set of random variables such that all the random variables sums to 1 – Joseph Dec 13 '17 at 13:29
  • Possible duplicate of [Generating a list of random numbers, summing to 1](https://stackoverflow.com/questions/18659858/generating-a-list-of-random-numbers-summing-to-1) – glibdud Dec 13 '17 at 13:31
  • @glibdud, the solution suggested there is to generate 7 random variable and divide b their sum to get the sum to 1 – Joseph Dec 13 '17 at 13:33
  • 1
    @Joseph You may need to explain a bit more what the desired properties are of your numbers. It appears to me that you're just trying to generate 7 random numbers that sum to 1, in an odd, roundabout sort of way. – glibdud Dec 13 '17 at 14:18

1 Answers1

0

To generate a sequence which has sum between 0 and 1 you can use this:

alphas = np.random.random(6)
alphas = alphas/sum(alphas)*np.random.rand()

alphas/sum(alphas) will make them sum to 1 and np.random.rand() will factor them between 0 and 1.

zipa
  • 27,316
  • 6
  • 40
  • 58
  • 1) your solution generates 6 random variables andand don't add an deduce the the seventh random variable. 2) the sum of your alphas is not equal to 1 – Joseph Dec 13 '17 at 13:52
  • I have explained which part is equal to 1 and which part changes it to be between 0 and 1. This solves your problem, and that is what SO is for, not for writing whole code. – zipa Dec 13 '17 at 14:24