0

I want to generate a list of N numbers in a set range, e.g. from 0 to 10. However, rather than having them evenly spaced out, I want to find an approach where the majority of newly generated values will be situated around a specific point between 0 and 10. For example either around one of the endpoints or around 8.5.

I tried doing so with numpy using numpy.logspace and numpy.linspace . numpy.linspace as expected returned the numbers evenly spaced, numpy.logspace and got back a skewed range, but all values are in a certain base (default is 10) if I log the resulting array like so:

numpy.log10(numpy.logspace(0,10)

I get evenly-spaced numbers once again. One approach can be setting a distribution for numbers to fall into but I am not sure how to do it.

sage
  • 57
  • 6
  • 4
    How about a triangular distribution, with left=0, right=10, mode=8.5 (or wherever you want the peak to be)? https://numpy.org/doc/stable/reference/random/generated/numpy.random.Generator.triangular.html – slothrop Feb 06 '23 at 13:15
  • Cant you just sample from e.g lognormal?https://numpy.org/doc/stable/reference/random/generated/numpy.random.lognormal.html – CutePoison Feb 06 '23 at 13:19
  • please check this post [link](https://stackoverflow.com/questions/46565585/random-numbers-in-a-range-around-a-median). I think it can near to your question. – Mohammad Nazari Feb 06 '23 at 13:50
  • @mkrieger1 I think this might work, I will try this, is it possible to center a distribution around some value? – sage Feb 06 '23 at 14:06
  • @CutePoison I think this is different. I need to push the distribution between a min and max. I couldn't see how to do it using lognormal as you can only set mean and sigma. I need something a bit more straighfroward. – sage Feb 06 '23 at 14:09
  • think I'd use a [beta distribution](https://en.wikipedia.org/wiki/Beta_distribution). e.g. `scipy.stats.beta.rvs(2, 8, size=100) * 10` would give 100 values in the range [0, 10] with an average of 2. playing with the first two parameters lets you control the mean and variance. beta variates are always in [0, 1], so the `* 10` just increases the range out to what you want. – Sam Mason Feb 06 '23 at 15:52

0 Answers0