-
Notifications
You must be signed in to change notification settings - Fork 7
/
Exercise 10.cpp
76 lines (61 loc) · 1.92 KB
/
Exercise 10.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// -----------------------------------------------------------------------------
// https://lptcp.blogspot.com/
//
/*
Chapter 24 - Exercise 10
How random is your default_random_engine? Write a program that takes two integers
n and d as inputs and calls randint(n) d times, recording the result. Output the
number of draws for each of [0:n] and "eyeball" how similar the counts are. Try
with low values for n and with low values for d to see if drawing only a few
random numbers causes obvious biases.
*/
// https://github.com/l-paz91/principles-practice/
// -----------------------------------------------------------------------------
//--INCLUDES--//
#include "std_lib_facilities.h"
// -----------------------------------------------------------------------------
void drawRandomNums(int pNumOfDraws, int pMaxRandNum)
{
system("CLS");
vector<int> histogram(pMaxRandNum+1);
for (int i = 0; i < pNumOfDraws; ++i)
{
++histogram[int(round(randint(pMaxRandNum)))];
}
for (int i = 0; i != histogram.size(); ++i)
{
cout << i << '\t';
for (int j = 0; j != histogram[i]; ++j)
{
cout << "*";
}
cout << '\n';
}
}
// -----------------------------------------------------------------------------
void fct()
{
while (true)
{
int numOfDraws = 0;
int maxRandNum = 0;
cout << "Num of draws: ";
cin >> numOfDraws;
cout << "Max rand num: ";
cin >> maxRandNum;
drawRandomNums(numOfDraws, maxRandNum);
}
}
// -----------------------------------------------------------------------------
int main()
{
// done to keep an eye on memory tracking (it will always report as a leak if done in main)
fct();
cout << "\n";
pressKeyToContinue();
_CrtDumpMemoryLeaks();
return 0;
}
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------