Mathematica,增加 2n+1,而不是 3

发布于 2024-10-28 20:21:35 字数 203 浏览 0 评论 0原文

这就是我所拥有的。

RandomChoice[{.5, .5} -> {Heads, Tails}, 3]

然后我写

RandomChoice[{.5, .5} -> {Heads, Tails}, 5]

如何以 2n+1 的增量增加这个函数?

Here's what I have.

RandomChoice[{.5, .5} -> {Heads, Tails}, 3]

then I write

RandomChoice[{.5, .5} -> {Heads, Tails}, 5]

How do I increase this function by increments of 2n+1?

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

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

发布评论

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

评论(2

我将您的请求解释为表明您想要函数 2n + 1,增量为 2。因此值将是:3、5、7、9、11...如果这是正确的解释,则以下内容应该有效:

Table[RandomChoice[{heads, tails}, 2 n + 1], {n, 1, 10}]

因此,对于 {1,2,3,4,5,6,7,8,9,10} 的域, f(n) = 2 n + 1

方式获得相同的结果:

Table[RandomChoice[{heads, tails}, n], {n, 3, 21, 2}]

您可以通过以下 增量是2。但是它不是同一个函数。目标与之前相同,但域现在与目标集相同:{3, 5, 7...21}

I interpreted your request as indicating you wanted the function, 2n + 1, with increments of 2. So the values would be: 3, 5, 7, 9, 11...If that is the correct interpretation then the following should work:

Table[RandomChoice[{heads, tails}, 2 n + 1], {n, 1, 10}]

So, f(n) = 2 n + 1 for the domain of {1,2,3,4,5,6,7,8,9,10}

You can achieve the same result with:

Table[RandomChoice[{heads, tails}, n], {n, 3, 21, 2}]

This makes it even clearer that the increment is 2. However it is not the same function. The target is the same as before but the domain is now the same as the target set: {3, 5, 7...21}

小帐篷 2024-11-04 20:21:35

您正在寻找这样的东西吗?

For[i = 1, i < 5, i++, Print[RandomChoice[{.5, .5} -> {Heads, Tails}, 2i+1]]]

这将给出一个示例输出:

{Heads, Tails, Tails}
{Tails, Tails, Heads, Heads, Tails}
{Tails, Tails, Heads, Tails, Heads, Heads, Tails}
{Heads, Heads, Tails, Tails, Heads, Tails, Heads, Tails, Tails}

这也做了同样的事情:

Do[Print[RandomChoice[{.5, .5} -> {Heads, Tails}, 2i+1]], {i, 4}]

我不知道是否有更优雅/更有效的方法来做到这一点,因为我是 Mathematica 的新手 - 但我确信有人会评论如果有。

Is something like this what you're looking for?

For[i = 1, i < 5, i++, Print[RandomChoice[{.5, .5} -> {Heads, Tails}, 2i+1]]]

Which would give a sample output of:

{Heads, Tails, Tails}
{Tails, Tails, Heads, Heads, Tails}
{Tails, Tails, Heads, Tails, Heads, Heads, Tails}
{Heads, Heads, Tails, Tails, Heads, Tails, Heads, Tails, Tails}

This also does the same thing:

Do[Print[RandomChoice[{.5, .5} -> {Heads, Tails}, 2i+1]], {i, 4}]

I don't know if there is a more elegant/efficient way of doing this, since I'm new to Mathematica - but I'm sure someone will comment if there is.

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