Error In passing a random number generator

Hi! I was running some Monte carlo simulations and I was having some trouble passing my Normal-Random-Generating object as a parameter to the simulator.

I created a class called BoxMull which used the Box-Muller algo to generate Normal randoms and I based the Box-Muller algo on random numbers generated by the Mersenne-twister engine.

I am getting an error while compiling and I cant figure it out.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <random>
#include <cmath>

const double pi = 3.1428;

template<class rnd_gen>
class BoxMull
{
    rnd_gen a;
    double range;
public://
    BoxMull(rnd_gen rnd)
    {
        a = rnd;
        range = rnd.max()-rnd.min();
    }
    double operator()()
    {
        static int flag = 0;
        static double n1;
        static double n2;
        if(flag == 0)
        {
            double U1 = a()/range;
            double U2 = a()/range;
            n1 = sqrt(-2*log(U1))*sin(2*pi*U2);
            n2 = sqrt(-2*log(U1))*cos(2*pi*U2);
            flag = 1;
            return n1;
        }
        else
        {
            flag = 0;
            return n2;
        }
    }
};

template<typename norm_gen>
class EulMaru
{
    double s0, mu, sig, T;
    norm_gen N;
public:
    EulMaru(double init_stock, double drift, double vol, double maturity, norm_gen n )
    {
        s0 = init_stock;
        mu = drift;
        sig = vol;
        T = maturity;
        N = n;
    }
    double operator()(double dt)//Returns a final stock price
    {
        double x = s0;
        int num_of_divs = T/dt;
        double sq_dt = sqrt(dt);
        for(int i = 1; i<=num_of_divs; ++i)
        {
            x *= 1 + mu*dt + sig*N()*sq_dt;
        }
        return x;
    }
};

int main ()
{
  std::mt19937 rr;//Object which generates random numbers using Mersenne twister
  BoxMull <std::mt19937> b(rr);//Object which generates (0,1) normal rvs based on rvs from mt engine
  EulMaru <BoxMull> e(50, 0.1, 0.3, 1.0, b);
  return 0;
}
Last edited on
Your issue is that you are not giving template parameters to BoxMull when you use it to instantiate EulMaru

EulMaru<BoxMull<std::mt19937>> e(50, 0.1, 0.3, 1.0, b);

EulMaru<decltype(b)> e(50, 0.1, 0.3, 1.0, b);
Last edited on
Hey buddy, Thanks for responding so fast. Your first line of code makes sense to me, so I was trying to make those changes. However I am still getting errors.

\\prism.nas.edu\aparab7\Documents\Desktop\cpptest.cpp||In instantiation of 'EulMaru<norm_gen>::EulMaru(double, double, double, double, norm_gen) [with norm_gen = BoxMull<std::mersenne_twister_engine<unsigned int, 32u, 624u, 397u, 31u, 2567483615u, 11u, 4294967295u, 7u, 2636928640u, 15u, 4022730752u, 18u, 1812433253u> >]':|
\\prism.nas.edu\aparab7\Documents\Desktop\cpptest.cpp|71|required from here|
\\prism.nas.edu\aparab7\Documents\Desktop\cpptest.cpp|47|error: no matching function for call to 'BoxMull<std::mersenne_twister_engine<unsigned int, 32u, 624u, 397u, 31u, 2567483615u, 11u, 4294967295u, 7u, 2636928640u, 15u, 4022730752u, 18u, 1812433253u> >::BoxMull()'|
\\prism.nas.edu\aparab7\Documents\Desktop\cpptest.cpp|47|note: candidates are:|
\\prism.nas.edu\aparab7\Documents\Desktop\cpptest.cpp|13|note: BoxMull<rnd_gen>::BoxMull(rnd_gen) [with rnd_gen = std::mersenne_twister_engine<unsigned int, 32u, 624u, 397u, 31u, 2567483615u, 11u, 4294967295u, 7u, 2636928640u, 15u, 4022730752u, 18u, 1812433253u>]|
\\prism.nas.edu\aparab7\Documents\Desktop\cpptest.cpp|13|note:   candidate expects 1 argument, 0 provided|
\\prism.nas.edu\aparab7\Documents\Desktop\cpptest.cpp|8|note: constexpr BoxMull<std::mersenne_twister_engine<unsigned int, 32u, 624u, 397u, 31u, 2567483615u, 11u, 4294967295u, 7u, 2636928640u, 15u, 4022730752u, 18u, 1812433253u> >::BoxMull(const BoxMull<std::mersenne_twister_engine<unsigned int, 32u, 624u, 397u, 31u, 2567483615u, 11u, 4294967295u, 7u, 2636928640u, 15u, 4022730752u, 18u, 1812433253u> >&)|
\\prism.nas.edu\aparab7\Documents\Desktop\cpptest.cpp|8|note:   candidate expects 1 argument, 0 provided|
\\prism.nas.edu\aparab7\Documents\Desktop\cpptest.cpp|8|note: constexpr BoxMull<std::mersenne_twister_engine<unsigned int, 32u, 624u, 397u, 31u, 2567483615u, 11u, 4294967295u, 7u, 2636928640u, 15u, 4022730752u, 18u, 1812433253u> >::BoxMull(BoxMull<std::mersenne_twister_engine<unsigned int, 32u, 624u, 397u, 31u, 2567483615u, 11u, 4294967295u, 7u, 2636928640u, 15u, 4022730752u, 18u, 1812433253u> >&&)|
\\prism.nas.edu\aparab7\Documents\Desktop\cpptest.cpp|8|note:   candidate expects 1 argument, 0 provided|
||=== Build failed: 1 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|



I think there is something wrong with the way I am using the mt19937 object, maybe.
Last edited on
Were you recently working in another language? You need to use the constructor initializer list:
46
47
48
49
50
51
52
53
    EulMaru(double init_stock, double drift, double vol, double maturity, norm_gen n )
    : s0(init_stock)
    , mu(drift)
    , sig(vol)
    , T(maturity)
    , N(n)
    {
    }
EDIT: This is my 9999th post!
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
template<typename norm_gen>
class EulMaru
{
    double s0, mu, sig, T;
    norm_gen N;
public:
    EulMaru(double init_stock, double drift, double vol, double maturity, norm_gen n)
    {
        s0 = init_stock;
        mu = drift;
        sig = vol;
        T = maturity;
        N = n;
    }
}


I did that...
Hey man LB...gotcha now...
However I am just curious , why is my constructor wrong?
Last edited on
Topic archived. No new replies allowed.