|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Problem 1209 - Average win for the player
You are presented with 3 closed boxes. You are going to pick one of the boxes and keep the money that's inside. Each of the 3 boxes contains either $3, $7 or $15, but you don't know which box contains what. Also in one of the boxes there is a slip of paper, in addition to the money, which has 'Go again' written on it. If you happen to pick that box, you get to repeat the procedure once again with the money and the piece of paper randomly distributed in the boxes. The game ends only if you happen to pick one of the 2 boxes without the piece of paper, so in theory the game could continue forever. Now then, what is the average win for the player? Don't give the answer in fractions, but as a numerical answer.
Solution Format/Example: 37.24
问题1209 -平均能赢多少(期望值)
给你3个封闭的盒子,盒子里放着3,7或15美元。但你不知道哪个盒子有多少钱。
你可以任意打开一个盒子,拿到里面的钱。
另外,3个盒子中有一个盒子随机放着一张纸条,写着“再来一次”。那你就可以再玩一次这个游戏。
直到你选择了另外2个没有放纸条的盒子,游戏就结束了。
理论上如果你运气好,一直猜到有纸条的盒子可以“再来一次”,那就可以一直拿到钱。
现在问,如果有无穷多的人来玩这个游戏,那么每一个玩家平均可以赢得多少钱?
答案格式 37.24
…………
这是一个蒙特卡洛随机模拟测试编程题目,很简单。
我的代码如下:
- Sub test()
- a = Array(3, 7, 15) '设置3个盒子放入钱
- For i = 0 To 10 ^ 6 - 1 '模拟1百万次
- n = 0 '连续次数归零
- Do
- n = n + 1
- t = Int(Rnd * 3) '选中1个盒子
- s = s + a(t) '累计获取金额
- r = Int(Rnd * 3) '随机放置再玩一次的纸条
- Loop While t = r '如果选中有纸条的盒子就再来一次
- If n > m Then m = n '统计最大连续次数
- Next
- MsgBox Round(s / i, 4) & vbCr & m '输出平均金额 以及最大连续次数
- End Sub
复制代码
结果=12.5、1百万次模拟时最大连续次数13-14次。
|
|