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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
#include <iostream>
#include <random>
#include <cstdlib> // abs, atoi
#include <cmath> // sqrt
using namespace std;
const double Pi = 3.14159265359;
default_random_engine Rnd(random_device{}());
void label(const char *s, int reps, int steps) {
cout << s << " (reps " << reps << ", steps " << steps << "): ";
}
// 1d: Gives an answer of about 80.
void mode_1d(int reps, int steps) {
label("1d", reps, steps);
uniform_int_distribution<int> direction(0, 1);
int sum = 0;
for (int rep = 0; rep < reps; ++rep) {
int n = 0;
for (int i = 0; i < steps; ++i)
n += (direction(Rnd) ? 1 : -1);
sum += abs(n);
}
cout << (double)sum / reps << '\n';
}
// 2d_orthogonal: Gives an answer of about 88.
void mode_2d_orthogonal(int reps, int steps) {
label("2d_orth", reps, steps);
uniform_int_distribution<int> direction(0, 1);
double sum = 0;
for (int rep = 0; rep < reps; ++rep) {
int x = 0, y = 0;
for (int i = 0; i < steps; ++i)
if (direction(Rnd)) x += (direction(Rnd) ? 1 : -1);
else y += (direction(Rnd) ? 1 : -1);
sum += sqrt(x * x + y * y);
}
cout << sum / reps << '\n';
}
// 3d_orthogonal: Gives an answer of about 92.
void mode_3d_orthogonal(int reps, int steps) {
label("3d_orth", reps, steps);
uniform_int_distribution<int> direction(0, 1), axis(0, 2);
double sum = 0;
for (int rep = 0; rep < reps; ++rep) {
int x = 0, y = 0, z = 0;
for (int i = 0; i < steps; ++i)
switch (axis(Rnd))
{
case 0: x += (direction(Rnd) ? 1 : -1); break;
case 1: y += (direction(Rnd) ? 1 : -1); break;
case 2: z += (direction(Rnd) ? 1 : -1); break;
}
sum += sqrt(x * x + y * y + z * z);
}
cout << sum / reps << '\n';
}
// 2d_all_directions: Gives an answer of about 89
void mode_2d_all_directions(int reps, int steps) {
label("2d_all", reps, steps);
uniform_real_distribution<> direction(0, 2 * Pi);
double sum = 0;
for (int rep = 0; rep < reps; ++rep) {
double x = 0, y = 0;
for (int i = 0; i < steps; ++i) {
double angle = direction(Rnd);
x += cos(angle);
y += sin(angle);
}
sum += sqrt(x * x + y * y);
}
cout << sum / reps << '\n';
}
/*
I'm not sure how to do this one.
// 3d_all_directions: Gives an answer of about
void mode_3d_all_directions(int reps, int steps) {
label("3d_all", reps, steps);
uniform_real_distribution<> direction(0, 2 * Pi);
double sum = 0;
for (int rep = 0; rep < reps; ++rep) {
double x = 0, y = 0, z = 0;
for (int i = 0; i < steps; ++i) {
double angle1 = direction(Rnd), angle2 = direction(Rnd);
x += ???;
y += ???;
z += ???;
}
sum += sqrt(x * x + y * y + z * z);
}
cout << sum / reps << '\n';
}
*/
int main(int argc, char **argv) {
int mode = 0, reps = 1000, steps = 10000;
if (argc >= 2) mode = atoi(argv[1]);
if (argc >= 3) reps = atoi(argv[2]);
if (argc >= 4) steps = atoi(argv[3]);
switch (mode) {
case 0: mode_1d (reps, steps); break;
case 1: mode_2d_orthogonal (reps, steps); break;
case 2: mode_3d_orthogonal (reps, steps); break;
case 3: mode_2d_all_directions(reps, steps); break;
//case 4: mode_3d_all_directions(reps, steps); break;
}
}
|