在R中模拟翻转炸薯条

发布于 2025-01-27 03:33:29 字数 1171 浏览 3 评论 0原文

我从小就一直有这个问题:

  • 假设您将100炸薯条放在炉子上,
  • 以解决这个问题,假设每个炸薯条只能有2个“状态”:“朝上”或“朝下” “
  • 每个炸薯条都需要在每一侧煮1分钟 - 如果在任何一侧煮过1分钟以上,则认为它被认为是燃烧的
  • ,将炸薯条放在锅中,一分钟后,您会摇晃泛 - 一些炸薯条在空中翻转并降落在锅上“朝上”或“朝下”,但有些炸薯条从来没有被翻转过来。
  • 又一分钟过去了,您再次摇动锅。
  • 为了这个问题,让我们假设每次摇动锅,每个炸薯条都有50%的机会在空中翻转,而在空中翻转的炸薯条都有50%的机会着陆的机会“朝下”或“朝上”。

这是一个问题:

  • 2分钟后,100个炸薯条中有多少个煮熟,燃烧了多少炸薯条?
  • 需要几分钟才能通过,直到保证所有炸薯条双方都煮过(即使其中许多会被烧毁)?

使用R,我尝试为这种情况写一个模拟:

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)

这就是我被卡住的地方 - 如果我可以以某种方式“标记”法语选择的薯条,我可以以50%的概率为这些炸薯条分配“烧伤/完美煮熟”的状态:

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))

如果我可以完成模拟,我可以扩展第二个翻转,第三个翻转等的模拟。在模拟中(例如5次翻转后),我可以绘制燃烧与完美烹饪的炸薯条数量的图表。然后,我可以多次重复模拟(例如1000次),并找出炸薯条的平均数量燃烧/煮熟。

有人可以告诉我如何写这个模拟吗?

谢谢你!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青巷忧颜 2025-02-03 03:33:29

用100个薯条制作数据框,并在第一侧累积时间。由于第二侧的时间仅仅是总时间 - 第一侧的时间,因此您可以将任何想要的测试应用于最终的“时间状态”。

dfrm <- data.frame(id=1:100, time=0)
set.seed(123) # good practice in simulation
#start

dfrm$time <- sample(c(0,60), 100, repl=TRUE)
# next minute
dfrm$time <- sample(c(0,60), 100, repl=TRUE)

sum(dfrm$time == 60)

# 54

随着时间==,您将有指数型腐烂的薯条比例==零或总时间,这将是“未完成”的组。由于仿真是“离散的”,有一段时间剩余的撤消概率要比任何指定的金额少,但是在保证时间内会很难。

Make dataframe with 100 fries and cumulative time on first side. Since time on second side is just total time - time on first side, you can apply any test you want to the final "time state".

dfrm <- data.frame(id=1:100, time=0)
set.seed(123) # good practice in simulation
#start

dfrm$time <- sample(c(0,60), 100, repl=TRUE)
# next minute
dfrm$time <- sample(c(0,60), 100, repl=TRUE)

sum(dfrm$time == 60)

# 54

You would have an exponentially decaying proportion of fries with time == to either zero or total time which would be the group which were "not done". Since the simulation is "discrete" there is a time when the probability of an remaining undone would be less that any specified amount but there would be difficulty with a guaranteed time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文