Sample a random number following a distribution between two values

450 views Asked by At

I would like to create a gaussian curve between two values (which is the end point of two different calculations). I am aware that i require a bit more background info, but i wish to play with the result. So let us assume what is not known, the values in question may be 1.9 and 2.1. enter image description here

My goals is to to draw a number randomly between those two, and be more biased to the mean than to the one or the other extreme as a start point for the next calculation.

Any help would be appreciated, thanks in advance!

UPDATE I:

for clarity,

both blue and black lines start at some origin, the calculation differs and the values at the end points are different. From only those two values, any amount of ppoints can be set up. n can be between 2 and 100 - it does not matter. The point is, to create a distribution like indicated by the bell shaped curve.

Following Severin Pappadeux's excellent suggestion, we now have defined n, which now leaves us only to define the distribution.

enter image description here

n=10

IH=1.9:0.01:2.1
v = 1.9 + ((2.1-1.9)/n) * IH(n)

bar(IH)
hold on
bar(n,v,'k')

UPDATE II: RESULTS!

As you can see, the curve multiplied into two curves, with each having a distribution of possible outcomes from which another calculation is started. The purpose was to be close to a setpoint. Im almost done, so

Big Thanks!

enter image description here

1

There are 1 answers

10
Severin Pappadeux On BEST ANSWER

Might use Irwin-Hall from https://en.wikipedia.org/wiki/Irwin%E2%80%93Hall_distribution

Basically,

min(IH(n)) = 0
max(IH(n)) = n
peak(IH(n)) = n/2

Scaling to your [1.9...2.1] range

v = 1.9 + ((2.1-1.9)/n) * IH(n)

It is bounded, very easy to sample, and at large n it is pretty much gaussian. You could vary n to get narrow or wide peak

Sampling, in some C pseudocode

double IH(int n) {
    double s = 0.0;
    for (int i = 0; i != n; ++i)
         s += uniform_random_number();

    return s;
}

UPDATE

translated to Octave

function rv = IH(n)
  rv = 0.0;
  for i = 1:n
    x  = rand;
    rv = rv + x;
  end
endfunction

So sampling would be along the line (say, one million events, IH with 8 d.o.f)

n = 8
for k = 1:1000000
  v = 1.9 + ((2.1-1.9)/n) * IH(n);
  % process v
end

Another update, added function QG, which generates desired random vector

function [x] = QG (n, k)
  for i = 1:k
    x(i) = 1.9 + ((2.1-1.9)/n)*IH(n);
  end
endfunction

Try to fill histrogram, along the lines

y = QG(8, 10000);
h = histogram(y);