-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGaltonMachine.cpp
More file actions
77 lines (71 loc) · 2.81 KB
/
GaltonMachine.cpp
File metadata and controls
77 lines (71 loc) · 2.81 KB
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
#include <iostream>
#include <cstdlib> // cnntains the system("pause") function
#include <random> // random numbres library
#include <string> // includes the string datatype
#include <conio.h> // includes key functions
using namespace std;
bool random_bool() {
// Create a static instance of std::random_device to seed the random number generator
static std::random_device rd;
// Create a static instance of std::mt19937 random number generator
static std::mt19937 gen(rd());
// Create a static instance of std::uniform_int_distribution to generate random integers between 0 and 1 (inclusive)
static std::uniform_int_distribution<> distrib(0, 1);
// Generate a random integer using the distribution and return true if it is 1, false otherwise
return distrib(gen) == 1;
}
void machine()
{
string columns[21] = {};
for (int i = 0; i < 300; i++) //this has to be the number of marbles
{
int reference = 0;
for (int k = 0; k < 20; k++)
{
bool random_result = random_bool();
if(random_result == true)
{
reference += 1;
}
else if (random_result == false)
{
reference -= 1;
}
}
reference = reference / 2; //for some reason we always get a pair number, so to get around that problem, we execute twice the random iterations (20), and then divide by 2, so we get a truly random number between -10 and 10
reference += 10;
columns[reference] += "#";
}
for (int c = 0; c < 21; c++)
{
cout << columns[c] << "\n";
}
cout << "\n\n\n\n\n\n\n\n\n\n\n";
}
int main()
{
cout << "Hello, my name is Miguel Veliz, and you are now going to see a simple implementation of the Galton Machine." << "\n";
cout << "But, what is a Galton Machine?, well, in few words, it is a bunch of marbles that have gone through a random process, but with an expected outcome." << "\n";
cout << "Now, we are going to see the same process in c++." << "\n";
cout << "Please, press the letter R in your keyboard to start a simulation. You can repeat this process many times. Press X in your keyboard to close the program." << "\n";
while (true) {
// Read input from the user
int key = getch();
// Check if a key was pressed
if (key != -1) {
// Run the appropriate function based on the key value
switch(key) {
case 'r':
machine();
break;
case 'x':
return 0;
break;
default:
// Do nothing for other keys
break;
}
}
}
system("pause");
}