0

I have list of numbers: h=[5, 6, 5, 6, 6, 6, 6, 6]. I have to use probability distribution to give diversity in this list 'h'. How can I add additional value between 0.3-2 for each number in the list by using probability distribution? so I will get some random value such as h=[5.3, 6.7, 6, 6.6, 6.3, 6.0, 7.1, 8]

jinha kim
  • 177
  • 5
  • 1
    It is unclear what you wan to do. Do you just want to draw a random number from some distribution and add it to each number in the existing list? If so, what distribution do you want to draw numbers from? Uniform? Normal? – AirSquid Jan 16 '20 at 04:15
  • I meant to use normal distribution – jinha kim Jan 16 '20 at 04:21

2 Answers2

0

If each value in that range is supposed to have the same probability, then you can do this:

import random
h = [ value + random.uniform(0.3, 2) for value in h ]

If you want to round to a single decimal like you did in your example, you can add a round function call in:

h = [ value + round(random.uniform(0.3, 2), 1) for value in h ]

If you want a normal distribution instead, it is unbounded by definition, so a strict range of values doesn't make sense. However, you can configure the standard deviation so that the vast majority of values are within a particular range. For example, you could do something like this:

low = 0.3
high = 2
mean = (low + high) / 2
standard_deviation = (high - mean) / 3
h = [ value + random.normalvariate(mean, standard_deviation) for value in h ]

Because we divide by 3 when calculating the standard deviation, the bounds (0.3 and 2) are 3 standard deviations away from the mean, which means that ~99.7% of values will be between 0.3 and 2. You could divide by 2 instead to make them 2 standard deviations away, which would make it so that ~95% of values are in that range.

mapeters
  • 1,067
  • 7
  • 11
0
import numpy as np

h = [5, 6, 5, 6, 6, 6, 6, 6]
result = [i+random.choice([round(j, 2) for j in np.arange(0.3, 2.1, 0.1)]) for i in h]
Afnan Ashraf
  • 174
  • 1
  • 14
  • Please put your answer always in context instead of just pasting code. See [here](https://stackoverflow.com/help/how-to-answer) for more details. – gehbiszumeis Jan 16 '20 at 06:26
  • this works too! I originally thought the same but I had to use normal distribution method. thanks though – jinha kim Jan 16 '20 at 06:32