您有更好的想法来模拟抛硬币吗?

发布于 2024-07-20 05:44:55 字数 147 浏览 5 评论 0 原文

现在我有

return 'Heads' if Math.random() < 0.5 

更好的方法吗?

谢谢

编辑:请忽略返回值,“更好”意味着确切的 50-50 概率。

Right now i have

return 'Heads' if Math.random() < 0.5 

Is there a better way to do this?

Thanks

edit: please ignore the return value and "better" means exact 50-50 probability.

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

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

发布评论

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

评论(10

り繁华旳梦境 2024-07-27 05:44:55

总有死简单的

coin = rand(1);

在许多脚本语言中,这将为您提供 0 和 arg 之间的随机 int,因此传递 1 将为您提供 0 或 1(正面或反面)。

there's always the dead simple

coin = rand(1);

in many scripting languages this will give you a random int between 0 and your arg, so passing 1 gives you 0 or 1 (heads or tails).

素手挽清风 2024-07-27 05:44:55

C 语言的数值食谱 表示不要信任内置的随机数生成器当重要的时候。 您可以将书中所示的算法实现为函数 ran1(),它声称该函数通过了所有已知的随机性统计测试(1992 年),调用次数少于 108 左右。

ran1() 算法背后的基本思想是向随机数生成器的输出添加洗牌以减少低阶序列相关性。 他们使用《计算机编程艺术》第 2 卷第 3.2-3.3 节中的 Bays-Durham shuffle,但我猜您可以使用 Fisher-Yates 洗牌也

如果您需要更多的随机值,同一文档还提供了一个生成器 (ran2),它应该适合至少 1017 值(我的猜测基于 2.3 x 10 的周期) 18)。 它还提供了一个函数(ran3),它使用不同的方法来生成随机数,以防线性同余生成器给您带来某种问题。

您可以将这些功能中的任何一个与您的 < 0.5 测试以更有信心获得均匀分布。

Numerical Recipes in C says not to trust the built in random number generators when it matters. You could probably implement the algorithm shown in the book as the function ran1(), which it claims passes all known statistical tests of randomness (in 1992) for less than around 108 calls.

The basic idea behind the ran1() algorithm is to add a shuffle to the output of the random number generator to reduce low order serial correlations. They use the Bays-Durham shuffle from section 3.2-3.3 in The Art of Computer Programming Volume 2, but I'd guess you could use the Fisher-Yates shuffle too.

If you need more random values than that, the same document also provides a generator (ran2) that should be good for at least 1017 values (my guess based on a period of 2.3 x 1018). The also provide a function (ran3) that uses a different method to generate random numbers, should linear congruential generators give you some sort of problem.

You can use any of these functions with your < 0.5 test to be more confident that you are getting a uniform distribution.

北笙凉宸 2024-07-27 05:44:55

向 xkcd 致敬:

string getHeadsOrTails {
        return "heads"; //chosen by fair coin toss,
                        //guaranteed to be random
    }

a wee homage to xkcd:

string getHeadsOrTails {
        return "heads"; //chosen by fair coin toss,
                        //guaranteed to be random
    }
你的呼吸 2024-07-27 05:44:55

你所拥有的就是我会做的方式。 如果 0.0 <= Math.random() < 1.0,按照标准,那么 (Math.random() < 0.5) 当 Math.random() 在 0.0 和 0.4999 之间时会给你正面......,当它在 0.5 和 0.999 之间时会给你反面......尽可能公平地掷硬币。

当然,我假设 Math.random() 有一个很好的实现。

What you have is the way I would do it. If 0.0 <= Math.random() < 1.0, as is standard, then (Math.random() < 0.5) is going to give you heads when Math.random() is between 0.0 and 0.4999..., and tails when it's between 0.5 and 0.999... That's as fair a coin flip as you can get.

Of course I'm assuming a good implementation of Math.random().

英雄似剑 2024-07-27 05:44:55

在 Linux 系统上,您可以从 /dev/random 读取位以获得“更好”的随机数据,但是像 Math.Random() 这样的几乎随机的方法几乎适用于您能想到的几乎所有应用程序,除非是严重的应用程序密码学工作。

On a linux system you could read bits in from /dev/random to get "better" random data, but an almost random method like Math.Random() is going to be fine for almost every application you can think of, short of serious cryptography work.

长伴 2024-07-27 05:44:55

尝试区分奇数和偶数。 另外,返回一个枚举值(或布尔值),而不是字符串。

Try differentiating between odd and even numbers. Also, return an enumeration value (or a boolean), rather than a string.

厌味 2024-07-27 05:44:55

我无法评论人们的帖子,因为我没有声誉,但只是关于整个 <= 与 <> 的仅供参考。 Bill The Lizard 的评论中提到的主题:因为可以有效地假设随机生成 0-1 之间的任何数字(由于浮点数大小的限制,技术上并非如此,但或多或​​少是正确的)在实践中) num <= .5 或 num <= .5 不会有区别 .5 因为在任何连续范围内获得任何一个特定数字的概率为 0。即:当 X = 0 和 1 之间的随机变量时,P(X=.5) = 0。

I can't comment on people's posts because I don't have the reputation, but just an FYI about the whole <= vs. < topic addressed in Bill The Lizard's comment: Because it can be effectively assumed that random is generating any number between 0-1 (which isn't technically the case due to limitations on the size of a floating point number, but is more or less true in practice) there won't be a difference in num <= .5 or num < .5 because the probability of getting any one particular number in any continuous range is 0. IE: P(X=.5) = 0 when X = a random variable between 0 and 1.

深海夜未眠 2024-07-27 05:44:55

这个问题唯一真正的答案是你无法“保证”概率。 如果你想一想,真正的硬币抛掷并不能保证 50/50 的概率,这取决于硬币、抛硬币的人,以及硬币是否掉落并滚过地板。 ;)

重点是它“足够随机”。 如果您正在模拟抛硬币,那么您发布的代码就非常好。

The only real answer to this question is that you cannot "guarantee" probability. If you think about it, a real coin flip is not guaranteed 50/50 probability, it depends on the coin, the person flipping it, and if the coin is dropped and rolls across the floor. ;)

The point is that it's "random enough". If you're simulating a coin flip then the code you posted is more than fine.

忘你却要生生世世 2024-07-27 05:44:55

这篇 github 帖子中有一个针对 bash 的紧凑解决方案:

(( RANDOM % 2 )) && {
    echo "heads" 
} || {
    echo "tails"
}

There's a compact solution for bash in this github post:

(( RANDOM % 2 )) && {
    echo "heads" 
} || {
    echo "tails"
}
雪化雨蝶 2024-07-27 05:44:55

尝试一下

return 'Heads' if Math.random() * 100 mod 2 = 0

,我真的不知道你使用的是什么语言,但如果随机数可以被二整除,那么它是正面,如果不是,那么它是反面。

Try

return 'Heads' if Math.random() * 100 mod 2 = 0

I don't really know what language you are using but if the random number is dividable by two then it is heads if it is not then it is tails.

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