-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
59 lines (54 loc) · 1.14 KB
/
main.cpp
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
//
// main.cpp
// GaussDistrib
//
// Created by Pawel Paruzel on 02/12/2017.
// Copyright © 2017 Pawel Paruzel. All rights reserved.
//
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cmath>
#include "Randomizer.hpp"
Randomizer r{}; // global randomizer
//Randomizer r{1995}; // fixed global randomizer
double gdist()
{
double min=-1., max=1.;
double delta = 0.2;
static double x = 0;
double x_;
for (int i{0}; i<100; ++i)
{
x_ = x + delta * r.range(min, max);
auto d = x*x - x_*x_;
if (d > 0 || r.range(0., 1.) < exp(d))
{
// accept
x = x_;
}
// else reject
}
return x;
}
int main(int argc, char* argv[])
{
std::string path = __FILE__;
if (argc > 1)
{
path.replace(path.end()-8, path.end(), argv[1]);
}
else
{
std::cerr << "No arguments passed!" << std::endl;
return 1;
}
std::ofstream file{path, std::ofstream::out};
constexpr int ITERS = 1e6;
for (int i{0}; i<ITERS; ++i)
{
file << gdist() << "\n";
}
file.close();
}