random number with ratio 1:2
-
04-07-2019 - |
Question
I have to generate two random sets of matrices Each containing 3 digit numbers ranging from 2 - 10
like that
matrix 1: 994,878,129,121
matrix 2: 272,794,378,212
the numbers in both matrices have to be greater then 100 and less then 999
BUT
the mean for both matrices has to be in the ratio of 1:2 or 2:3 what ever constraint the user inputs
my math skills are kind of limited so any ideas how do i make this happen?
Solution
In order to do this, you have to know how many numbers are in each list. I'm assuming from your example that there are four numbers in each.
- Fill the first list with four random numbers.
- Calculate the mean of the first list.
- Multiply the mean by 2 or by 3/2, whichever the user input. This is the required mean of the second list.
- Multiply by 4. This is the required total of the second list.
- Generate 3 random numbers.
- Subtract the total of the three numbers in step 5 from the total in step 4. This is the fourth number for the second list.
- If the number in step 6 is not in the correct range, start over from step 5.
Note that the last number in the second list is not truly random, since it's based on the other values in the list.
OTHER TIPS
You have a set of random numbers, s1.
s1= [ random.randint(100,999) for i in range(n) ]
For some other set, s2, to have a different mean it's simply got to have a different range. Either you select values randomly from a different range, or you filter random values to get a different range.
No matter how many random numbers you select from the range 100 to 999, the mean is always just about 550. The odds of being a different value are exactly the normal distribution probabilities on either side of the mean.
You can't have a radically different mean with values selected from the same range.