I'm practicing my R coding today and am working on the problem where you have a stick with length=1 and break it into 3 pieces. We need to figure out what the probability is that those 3 broken pieces will form a triangle. I've found some proper code online for this problem, but they are slightly beyond my understanding.
Why won't this code work? Could you tell me how to fix it? TIA!
set.seed(12345)
breaks <- sort(runif(n=2))
stick_lengths <- c(breaks[1], breaks[2]-breaks[1], 1-breaks[2])
stick_lengths
repetitions <- 10000
tri <- c(1:10000)
trialstotri <- 0
for(i in 1:repetitions){
break1 <- 0
trials <- 0
while(breaks[1]+(breaks[2]-breaks[1])> 1-breaks[2] TRUE){
breaks <- sample(tri, size=1, replace=TRUE)
trialstotri <- trialstotri + 1 }
print(proportions(TRUE))
}
Here's a structure that I think will work, and I'll leave some details for you to fill in.
repetitions <- 10000
trials <- logical(length = repetitions)
set.seed(12345)
for(i in 1:repetitions) {
# break a stick randomly
breaks <- sort(runif(n=2))
stick_lengths <- c(breaks[1], breaks[2]-breaks[1], 1-breaks[2])
# see if it works as a triangle
if(
(stick_lengths[1] + stick_lengths[2] > stick_lengths[3])
&
() & () ## fill in additional conditions! One is not enough!
) {
trials[i] <- TRUE
} else {
trials[i] <- FALSE
}
}
mean(trials)