# triangle problemprob.tri <-function(n =10000) {
total <-0# total trial times j <-0# loop indexwhile (j <= n) {
# flag: `is.tri = 1` means it can be a triangle is.tri <-1# select 3 random number from uniform-distribution vec <-runif(3)
# to tell if it's possible to form a triangle# (a+b>c, b+c>a, c+a>b)for (i in 1:3) {
if (vec[i] >sum(vec[-i])) {
is.tri <-0 }
}
total <- total + is.tri
j <- j +1 }
# calculation of probability of successful triangles total / n
}
# 进行 10 轮试验,看看结果的分布情况mult.result <-c()
for (i in 1:10) {
mult.result <-rbind(mult.result, prob.tri())
}
print(mult.result)