请教我如何重复和列出-Mathematica

发布于 2024-10-29 16:05:28 字数 592 浏览 1 评论 0原文

我该如何循环这个?

p = Table[RandomChoice[{Heads, Tails}, 2 i + 1], {i, 10}];
v = Count[#, Heads] & /@ p;
c = Count[#, Tails] & /@ p;
f = Abs[v - c];
g = Take[f, LengthWhile[f, # != 3 &] + 1]

谢谢!

编辑

在这个抛硬币游戏中,规则如下:

  • 单次游戏包括重复 抛一枚公平的硬币,直到 数量之间的差异 抛出的头和尾部的数量 是三。
  • 每次硬币您必须支付 1 美元 翻转,并且您可能无法退出 游戏的进行。
  • 每次结束时您都会收到 8 美元 游戏的玩法。

    1. 你应该玩这个游戏吗?
    2. 您预计会赢得多少奖金或 玩了 500 次就输了?

您可以使用电子表格模拟和/或概率推理来回答这些问题。

课程使用 Excel,我正在尝试学习 Mathematica。

How do I loop this?

p = Table[RandomChoice[{Heads, Tails}, 2 i + 1], {i, 10}];
v = Count[#, Heads] & /@ p;
c = Count[#, Tails] & /@ p;
f = Abs[v - c];
g = Take[f, LengthWhile[f, # != 3 &] + 1]

Thanks!

EDIT

In this coin flipping game the rules are as follows :

  • A single play consists of repeatedly
    flipping a fair coin until the
    difference between the number of
    heads tossed and the number of tails
    is three.
  • You must pay $1 each time the coin is
    flipped, and you may not quit during
    the play of the game.
  • You receive $8 at the end of each
    play of the game.

    1. Should you play this game?
    2. How much might you expect to win or
      lose after 500 plays?

You may use a spreadsheet simulation and/or reasoning about probabilities to answer these questions.

The class is using Excel, I'm trying to learn Mathematica.

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

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

发布评论

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

评论(5

也只是曾经 2024-11-05 16:05:28

理论方面多一点

你的游戏是随机游走< /strong> 位于 R1 上。

因此,达到 3 距离的翻转次数的期望值是 32=9,这也是您的成本的期望值。

由于您每场比赛的收入为 8 美元,您将以每场比赛的平均损失率为 1 美元。

请注意,这些数字与 @Mr. 一致。 15000 局游戏的向导结果为 135108 - 120000 = 15108

A little bit more on the theoretical side

Your game is a random walk on R1.

As such, the expectancy value for the number of flips to get a distance of 3 is 32=9, and that is also the expectancy value for your cost.

As your earning per game is $8, you'll lose at a mean rate of $1 per game.

Note that these figures are consistent with @Mr. Wizard's result of 135108 - 120000 = 15108 for 15000 games.

深海里的那抹蓝 2024-11-05 16:05:28

如果我了解抛硬币游戏的规则,并且如果必须使用蒙特卡洛方法,请考虑以下内容:

count = 
  Table[
    i = x = 0;
    While[Abs[x] < 3, x += RandomChoice[{1, -1}]; i++];
    i,
    {15000}
  ];

这个想法是抛一枚硬币,直到一个人赢了三分,然后输出得到的轮数那里。执行此操作 15,000 次,并创建结果列表 (count)。

您玩 15,000 场游戏所花费的钱只是玩的回合数,或者:

Total @ count

(* Out=  135108 *)

虽然您的奖金为 8 美元 * 15,000 = 120,000 美元,所以这不是一个好玩的游戏。

如果需要统计每个圈数出现的次数,则:

Sort @ Tally @ count

If I understand the rules of the coin flipping game, and if you must use a Monte Carlo method, consider this:

count = 
  Table[
    i = x = 0;
    While[Abs[x] < 3, x += RandomChoice[{1, -1}]; i++];
    i,
    {15000}
  ];

The idea is to flip a coin until one person is winning by three, and then output the number of turns it took to get there. Do this 15,000 times, and create a list of the results (count).

The money you spent to play 15,000 games is simply the number of turns that were played, or:

Total @ count

(* Out=  135108 *)

While your winnings are $8 * 15,000 = $120,000, so this is not a good game to play.

If you need to count the number of times each number of turns comes up, then:

Sort @ Tally @ count
水染的天色ゝ 2024-11-05 16:05:28

不确定这是否是完成您想要的事情的最佳方式,但这应该可以帮助您开始。首先,请注意,我将名称 Heads 和 Tails 更改为小写(Heads 是内置符号...)——小写变量名称是避免此类问题的最佳方法。

Remove[p, v, c, fun, f, g, head, tail];
fun[n_] :=
 Do[
  Block[
   {p, v, c, f, g},
   p = Table[RandomChoice[{head, tail}, 2 i + 1], {i, 10}];
   v = Count[#, head] & /@ p;
   c = Count[#, tail] & /@ p;
   f = Abs[v - c];
   g = Print[Take[f, LengthWhile[f, # != 3 &] + 1]]
   ],
  {n}]

只需输入您想要运行循环的次数... fun[5] 给出:

{1,1,1,1,5,3}

{3}

{1,1,5,1,5,1,3}

{3}

{1,5,3}

注意:因为您可能想对输出执行某些操作,所以使用 Table[] 可能比 Do[] 更好。这将返回一个列表列表。

Remove[p, v, c, fun, f, g, head, tail];
fun[n_] :=
 Table[
  Block[
   {p, v, c, f, g},
   p = Table[RandomChoice[{head, tail}, 2 i + 1], {i, 10}];
   v = Count[#, head] & /@ p;
   c = Count[#, tail] & /@ p;
   f = Abs[v - c];
   g = Take[f, LengthWhile[f, # != 3 &] + 1]
   ],
  {n}]

没什么花哨的!

Not sure if this is the best way to accomplish what you want, but this should get you started. First, note that I changed the names Heads and Tails to lowercase (Heads is a built-in symbol...)---lowercase variable names are the best way to avoid this type of problem.

Remove[p, v, c, fun, f, g, head, tail];
fun[n_] :=
 Do[
  Block[
   {p, v, c, f, g},
   p = Table[RandomChoice[{head, tail}, 2 i + 1], {i, 10}];
   v = Count[#, head] & /@ p;
   c = Count[#, tail] & /@ p;
   f = Abs[v - c];
   g = Print[Take[f, LengthWhile[f, # != 3 &] + 1]]
   ],
  {n}]

Simply enter the number of times you want to run the loop... fun[5] gives:

{1,1,1,1,5,3}

{3}

{1,1,5,1,5,1,3}

{3}

{1,5,3}

Note: because you'll probably want to do something with the output, using Table[] is probably better than Do[]. This will return a list of lists.

Remove[p, v, c, fun, f, g, head, tail];
fun[n_] :=
 Table[
  Block[
   {p, v, c, f, g},
   p = Table[RandomChoice[{head, tail}, 2 i + 1], {i, 10}];
   v = Count[#, head] & /@ p;
   c = Count[#, tail] & /@ p;
   f = Abs[v - c];
   g = Take[f, LengthWhile[f, # != 3 &] + 1]
   ],
  {n}]

Nothing fancy!

酷炫老祖宗 2024-11-05 16:05:28

更像 Mathematica 一点。没有定义变量。

g[n_] := Table[(Abs /@ Total /@ 
             Array[RandomChoice[{-1, 1}, (2 # + 1)] &, 10]) /.
                                        {x___, 3, ___} :> {x, 3}, 
          {n}]  

感谢 @Mr.Wizard 此答案

g[2]
->{{1, 1, 1, 5, 5, 1, 5, 7, 3}, {1, 3}}

A little more Mathematica-ish. No vars defined.

g[n_] := Table[(Abs /@ Total /@ 
             Array[RandomChoice[{-1, 1}, (2 # + 1)] &, 10]) /.
                                        {x___, 3, ___} :> {x, 3}, 
          {n}]  

Credit to @Mr.Wizard for this answer.

g[2]
->{{1, 1, 1, 5, 5, 1, 5, 7, 3}, {1, 3}}
七堇年 2024-11-05 16:05:28

我不喜欢抱怨 RTFM 等,但循环非常基本。如果我在文档中心的搜索框中输入“loop”,前几个点击之一包含指向“guide/LoopingConstructs”页面的链接,并且包含指向教程“tutorial/LoopsAndControlStructures”的链接。你读过这些吗?

I don't like bitching about RTFM etc. but looping is pretty basic. If I type "loop" in the search box in the documentation center one of the first few hits contains a link to the page "guide/LoopingConstructs" and this contains a link to the tutorial "tutorial/LoopsAndControlStructures". Have you read these?

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