What you are looking for is most easily done using the so-called reception rejection method.
Divide your interval into smaller intervals. Specifying a probability density function (PDF) can be very simple as a step function. For a Gaussian distribution, you would have left and right steps below your average step ie (see Image below, which has a more general distribution).

Generate a random number throughout the interval. If the generated number is greater than the value of your PDF at this point, reject the generated number.
Repeat the steps until you get the desired number of points.
EDIT 1
Proof of the concept in Gaussian PDF.
So, the main idea is shown in graph (a).
- / (PDF). PDF x /. PDF
x, : 1) f(x) >= 0 2) ( , 1). - (
max) " " (z1 < z2) PDF. PDF . (z1, z2), PDF(z1>x>z2) < eta, eta . , ≤ t26 > , , - , PDF(x) , . Ch(z1, z2, max) . , .- x ,
z1<x<z2. y (0, max). y , PDF(x), (x,y) 4. y , PDF(x), x return it.
, PDF.
#include "Random.h"
#include <fstream>
using namespace std;
double gaus(double a, double b, double c, double x)
{
return a*exp( -((x-b)*(x-b)/(2*c*c) ));
}
double* random_on_a_gaus_distribution(double inter_a, double inter_b)
{
double res [2];
double a = 1.0;
double b = 2.0;
double c = 3.0;
double x = kiss::Ran(inter_a, inter_b);
double y = kiss::Ran(0.0, 1.0);
while (y>gaus(a,b,c,x))
{
x = kiss::Ran(inter_a, inter_b);
y = kiss::Ran(0.0, 1.0);
}
res[0] = x;
res[1] = y;
return res;
}
void main()
{
double* x;
ofstream f;
f.open("test.txt");
for(int i=0; i<100000; i++)
{
x = random_on_a_gaus_distribution(-5.0, 10.0);
f << x[0]<<","<<x[1]<<endl;
}
f.close();
}
1
, PDF , gaus. .
random_on_a_gaus_distribution, . \ a, b, c, . (1, 2, 3) , , HW ( : , , 7000).
2 3
wolfram mathematica gaus. 1,2,3 , max (z1, z2). . 1.0 , eyeballin ' , - 5,0 10,0.
random_on_a_gaus_distribution , 2) eta, , PDF , . , , . , . . . , "". - , , .
4 5
bash . , . x - . x x .
x x_max x, , PDF(x) < PDF(x_max).
, , PDF x , , xi, PDF(xi)<PDF(x).
x, y, , , , x. matplotlib.

, . , x, PDF, , .

, , . . , (mersene twister ).
Random.h
#pragma once
#include <stdlib.h>
const unsigned RNG_MAX=4294967295;
namespace kiss{
unsigned int RanUns();
void RunGen();
double Ran0(int upper_border);
double Ran(double bottom_border, double upper_border);
}
namespace Crand{
double Ran0(int upper_border);
double Ran(double bottom_border, double upper_border);
}
Kiss.cpp
#include "Random.h"
unsigned int kiss_z = 123456789;
unsigned int kiss_w = 378295763;
unsigned int kiss_jsr = 294827495;
unsigned int kiss_jcong = 495749385;
unsigned int kiss::RanUns()
{
kiss_z=36969*(kiss_z&65535)+(kiss_z>>16);
kiss_w=18000*(kiss_w&65535)+(kiss_w>>16);
kiss_jsr^=(kiss_jsr<<13);
kiss_jsr^=(kiss_jsr>>17);
kiss_jsr^=(kiss_jsr<<5);
kiss_jcong=69069*kiss_jcong+1234567;
return (((kiss_z<<16)+kiss_w)^kiss_jcong)+kiss_jsr;
}
void kiss::RunGen()
{
for (int i=0; i<2000; i++)
kiss::RanUns();
}
double kiss::Ran0(int upper_border)
{
unsigned velicinaIntervala = RNG_MAX / upper_border;
unsigned granicaIzbora= velicinaIntervala*upper_border;
unsigned slucajniBroj = kiss::RanUns();
while(slucajniBroj>=granicaIzbora)
slucajniBroj = kiss::RanUns();
return slucajniBroj/velicinaIntervala;
}
double kiss::Ran (double bottom_border, double upper_border)
{
return bottom_border+(upper_border-bottom_border)*kiss::Ran0(100000)/(100001.0);
}
, C:
CRands.cpp
#include "Random.h"
double Crand::Ran0(int upper_border)
{
return rand()%upper_border;
}
double Crand::Ran (double bottom_border, double upper_border)
{
return (upper_border-bottom_border)*rand()/((double)RAND_MAX+1);
}
(b). PDF, PDF(x) .
, Ch(x) PDF-, y PDF(x); - ! , y , PDF(x) . , , , , max PDF.
Ch(x) , . .
? ? , ? max , , , .
, , , Ch(x) , PDF .
, , , y . z 0 1 lower_height/higher_height, < 1. z , : accept x .
, . , .. function, , eval , , max/min / , , .
!