使用Rand3()的随机生成器Rand9() [英] Random generator Rand9() using Rand3()

查看:284
本文介绍了使用Rand3()的随机生成器Rand9()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用生成1到3之间的数字的函数来生成1到9之间的数字?获得1到9之间的任何数字的概率必须相同,因此rand3()+ rand3()+ rand3()不是一个好的解决方案.

How can I generate a number between 1 and 9 using a function which generate a number between 1 and 3? The probability to obtain any number between 1 and 9 must be the same, so rand3()+rand3()+rand3() is not a good solution.

推荐答案

尝试类似笛卡尔的乘积:

Try cartesian-like product:

Rand9() = 3 * (Rand3() - 1) + Rand3()

使用3 *(Rand3()-1)可使子间隔1-3、4-6和7-9发生的可能性相等.然后,使用+ Rand3(),您可以在该子间隔上平均选择.

With 3 * (Rand3() - 1) you make sub-intervals 1-3, 4-6 and 7-9 equally likely. With + Rand3() you will then choose equally on that sub-interval.

作为产品撰写:

3 * (Rand3() - 1) + Rand3() -> {1, 4, 7} X {+1,+2,+3} -> {1,2,3,4,5,6,7,8,9}

这篇关于使用Rand3()的随机生成器Rand9()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆