-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriangle.php
54 lines (40 loc) · 885 Bytes
/
triangle.php
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
<?php
// a line ,randomly cut into 3 segments, to calculate what is probablity
// the 3 segments can form a triangle.
$len = 650;
$total = 10000000;
$sub = 0;
$temp = $total;
while($temp > 0) {
$p1 = rand(1, $len);
$p2 = rand(1, $len);
$seg1 = null;
$seg2 = null;
$seg3 = null;
if ($p1 > $p2) {
$t = $p1;
$p1 = $p2;
$p2 = $t;
}
$seg1 = $p1;
$seg2 = $p2-$p1;
$seg3 = $len-$p2;
//var_dump($p1, $p2);
$s1 = false;
$s2 = false;
$s3 = false;
if ($seg3 > 0 && $seg3 < ($seg1+$seg2)) {
$s1 = true;
}
if ($seg1 > 0 && $seg1 < ($seg2 + $seg3)) {
$s2 = true;
}
if ($seg2 > 0 && $seg2 < ($seg1 + $seg3)) {
$s3 = true;
}
if ($s1 && $s2 && $s3) {
$sub += 1;
}
$temp -= 1;
}
echo ($sub / $total) . "\n";