-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProbablityOfAStickMakingTriangle.cpp
67 lines (50 loc) · 1.82 KB
/
ProbablityOfAStickMakingTriangle.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
// Consider a stick of length 1. Pick two points uniformly at random on the stick,
// and break the stick at those points. What is the probability that the
// three segments obtained in this way form a triangle?
//
// See: http://www.cut-the-knot.org/Curriculum/Probability/TriProbCartesian.shtml
//
// Compiler: Microsoft Visual C++ Compiler Nov 2012 CTP (v120_CTP_Nov2012)
#include <iostream>
#include <chrono>
#include <random>
const unsigned int NUM_CALCULATIONS = 10000000;
typedef std::chrono::high_resolution_clock Clock;
typedef std::chrono::milliseconds milliseconds;
int main()
{
float pointA, pointB, temp;
unsigned int canMakeTriangle = 0;
std::default_random_engine generator((unsigned int)time(0));
std::uniform_real_distribution<float> distribution(0.0, 1.0);
std::cout << "Starting brute force probability estimate." << std::endl;
Clock::time_point t0 = Clock::now();
for (auto i = 0; i <= NUM_CALCULATIONS ; ++i)
{
pointA = distribution(generator);
pointB = distribution(generator);
// |--A---B--|
if (abs(pointA - pointB) > 0.5)
continue;
// Swap variables.
if (pointA > pointB)
{
temp = pointA;
pointA = pointB;
pointB = temp;
}
// |--A--B---| |---A--B--|
if (pointB < 0.5 || pointA > 0.5)
continue;
++canMakeTriangle;
}
Clock::time_point t1 = Clock::now();
auto answer = (float) canMakeTriangle / (float) NUM_CALCULATIONS;
std::cout << std::endl << "Probability that triangle can be made is: " << canMakeTriangle << " / " << NUM_CALCULATIONS << " = " << answer << std::endl;
milliseconds ms = std::chrono::duration_cast<milliseconds>(t1 - t0);
auto throughput = NUM_CALCULATIONS / ms.count();
std::cout << std::endl << "Throughput was: " << throughput << " Calc. / ms." << std::endl;
std::cout << "Press ENTER to exit." << std::endl;
std::cin.get();
return 0;
}