Hi everyone,
My purpose is to fill a matrix with random numbers. As you can see in the code, I defined a matrix called sim_matrix (which should be filled), but the dimensions of such matrix is given by the parameters that we introduce in the constructor. Because we don't know the size of such matrix until we give these parameters through the constructor. I'd like to know how to declare such matrix, or how to modify the size,... Does anyone of you know how to do that?
/*
* MonteCarlo.cpp
*
* Created on: 17/04/2013
* Author: carlos
*/
#include <iostream>
#include<fstream>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/normal_distribution.hpp>
#include "MonteCarlo.h"
#include "BlackScholes.h"
usingnamespace std;
MonteCarlo::MonteCarlo(int n_paths, int n_simulations)
{
//number paths
set_np(n_paths);
//number steps
set_nt(n_simulations);
//now we simulate and save the random normal number in the matrix sim_matrix[][]
//the two elements get_np and get_nt is setting the matrix size
simulate(get_np(),get_nt());
}
MonteCarlo::~MonteCarlo() { }
//setters and getters methods
void MonteCarlo::set_np(int a){np=a;}
int MonteCarlo::get_np(){return np;}
void MonteCarlo::set_nt(int b){nt=b;}
int MonteCarlo::get_nt(){return nt;}
double MonteCarlo::get_matrix(int row, int col){return sim_matrix[row][col];}
//we fill the matrix sim_matrix with random numbers (normal distributed)
void MonteCarlo::simulate(int a, int b)
{
//It will generate the same values...not problematic for the moment
boost::random::mt19937 gen;
//setting the boost for a normal distribution de ~N(0,1)
boost::random::normal_distribution<> dist(0.0, 1.0);
for (int i=0;i<a;i++)
{
for(int j=0;j<b;j++)
{
//setting the random number to the matrix, indexes i for row and j for colum
sim_matrix[i][j] = dist(gen);
}
}
}
I'll be very thankful if someone of you try to help me with this topic.