# Simulate Flipping French Fries in R

I always had this question since I was a kid:

• Suppose you place 100 french fries on to a pan over a stove
• For this problem, let's assume that each french fry can only have 2 "states" : "face up" or "face down"
• Each french fry needs to be cooked for 1 minute on each side - if a french fry is cooked for more than 1 minute on any side, it is considered as burnt
• You place the french fries on the pan and after one minute you shake the pan - some of the french fries get flipped in the air and land on the pan either "face up" or "face down", but some of the french fries never got flipped at all.
• After another minute has passed, you shake the pan again.
• For the sake of this question, let's assume that each time you shake the pan, each individual french fry has a 50% chance of getting flipped in the air, and the french fries that were flipped in the air have a 50% chance of landing "face down" or "face up".

Here is the question:

• After 2 minutes, how many of the 100 french fries are perfectly cooked and how many french fries are burnt?
• How many minutes need to pass until all french fries are guaranteed to have been cooked on both sides (even though many of them will be burnt)?

Using R, I tried to write a simulation for this scenario:

``````original_data = data.frame(id = 1:100, state = "start")

number_fries_selected_in_first_flip = sample(1:100, 1, replace=F)

fries_selected_in_first_flip = sample(1:100, number_fries_selected_in_first_flip, replace=F)
``````

This is where I got stuck - if I could somehow "tag" the french fries that were selected, I could assign a "burnt/perfectly cooked" status to these french fries with 50% probability:

``````status <- c("perfectly cooked","burnt")

original_data\$tagged_fries_status <- sample(status, number_fries_selected_in_first_flip, replace=TRUE, prob=c(0.5, 0.5))
``````

If I could finish the simulation, I could extend the simulation for the second flip, third flip, etc. At the end of the simulation (e.g. after 5 flips), I could make a chart of the number of french fries that are burnt vs perfectly cooked. Then, I could repeat the simulation many times (e.g. 1000 times), and find out the average number of fries burnt/perfectly cooked.

Could someone please show me how to write this simulation?

Thank you!

I played with it:

``````library(tidyverse)

#consider 0 raw,
#         1  cooked p,
#        and 2+ burned
(original_data = tibble(fry_id = 1:100,
side_0 = 0,
side_1= 0, side = 0))
set.seed(42)

cook_then_shake <- function(df){
#cook
mutate(df,
side_0 = ifelse(side==0,
side_0+1,side_0),
side_1 = ifelse(side==1,
side_1+1,side_1),
#shake
side=sample(0:1,nrow(df),replace = TRUE)
)

}

# do a 100 2 shake experiments

hundred_2shakes <- map(1:100,
~original_data %>% cook_then_shake %>% cook_then_shake)

#franction of cooked (1,1)
(pcooked_portion_of_hundred_2shakes <- map_int(hundred_2shakes,
~filter(.,
side_0==1 ,
side_1==1) %>% nrow) )
hist(pcooked_portion_of_hundred_2shakes)
mean(pcooked_portion_of_hundred_2shakes)

experiment_to_completion <- function(quiet=TRUE){
uncooked <- Inf
experiment_data <- original_data
minute_count <- 0L
while(uncooked>0){
experiment_data <- cook_then_shake(experiment_data)
minute_count <- minute_count + 1L
uncooked <- nrow(filter(experiment_data,
side_0==0 | side_1 == 0))
if(!quiet){  cat("After ", minute_count, " remaining uncooked: ", uncooked,"\n")
}}
cat(".")
return(minute_count)
}

#test it
experiment_to_completion(quiet = FALSE)
# get results from 100 runs

(exp_to_comp_1k <- map_int(1:100,~experiment_to_completion()))

hist(exp_to_comp_1k)
mean(exp_to_comp_1k)
range(exp_to_comp_1k)``````
2 Likes

probably fun to code, but no need of a simulation here, basic maths work well enough...
each flip gives each fries a 25% chance to flip.

• after 2 min, you get (on average) 25% of fries perfectly cooked, all others are burnt.
• you will never have a guarantee that all fries were cooked on both sides, if you rephrase on how much time until you get a probablity of 95% of all fries were cooked on one side, then one calculate it ... 