- #1
schniefen
- 178
- 4
Studying different ways to generate random numbers according to a distribution and the below algorithm describes the "box method". A search on Google led to the Box-Mueller method. Are they related? Also, what would be a simple implementation of this algorithm for ##f(x)=\sin{(x)}## on ##[0,\pi]##? Is it correct that ##y## should be scaled to some value in ##[0,1]##?
Implementation example:
We generate two random numbers ##x## and ##y##. We scale ##x## so that it
gives a random point in the restricted range we want to generate random numbers in. Now we scale
##y## so it matches the range from 0 to the maximum value of the function ##f(x)## we want to generate.
Now we accept ##x## if ##y < f(x)## and reject ##x## otherwise.
gives a random point in the restricted range we want to generate random numbers in. Now we scale
##y## so it matches the range from 0 to the maximum value of the function ##f(x)## we want to generate.
Now we accept ##x## if ##y < f(x)## and reject ##x## otherwise.
Implementation example:
C++:
#include <math.h>
int main(){
double x = (double)rand();
double y = (double)rand();
double xs = fmod(x,pi);
double ys = fmod(y,1);
if (ys < sin(xs)) return sin(xs);
}