I have developed the following code to generate random numbers from an uniform distribution and using these calculate the samples from a normal distribution.
First I only used the randuniform function and it worked, it printed in the console the numbers generated as expected, but when I include the boxmuller function it simply does not print anything and so I don’t know if it’s working properly, it doesn’t raise any error too.
Do anyone know what’s going on and why its not printing in the console?
#include <random>
#include <iostream>
#include <stdlib.h>
#include <vector>
using namespace std;
vector<double> randuniform(int n) {
vector<double> Rand;
for(int i; i < n; i++) {
Rand.push_back((double(rand())/double(RAND_MAX)));
cout << Rand[i] << endl;
}
return Rand;
}
vector<double> boxmuller(vector <double>& Rand, int n) {
vector<double> Normal;
for (int i; i<(n-1); i+=2) {
double n1 = sqrt(-2*log(Rand[i]))*cos(2*3.1415*Rand[i+1]);
double n2 = sqrt(-2*log(Rand[i+1]))*sin(2*3.1415*Rand[i+1]);
Normal.push_back(n1);
Normal.push_back(n2);
cout << n1 << endl;
cout << n2 << endl;
}
return Normal;
}
int main() {
int n = 5;
vector<double> Rand = randuniform(n);
cout << Rand[1] << endl;
vector<double> Normal = boxmuller(Rand, n);
}