Redian新闻
>
【ms面试题】用不均匀的骰子掷出均匀的点数
avatar
【ms面试题】用不均匀的骰子掷出均匀的点数# JobHunting - 待字闺中
m*d
1
怎么样用一个不均匀的筛子。比如 1/3 概率是1,1/3概率是2, 1/12 是3 ,1/12 是4.
1/12 是5,1/12 是6, 掷出均匀的点数。
问题 用这个筛子写一个ramdon函数, 返回的数字1-6 是均匀出现的 。
avatar
r*y
2
当投到1和2的时候,有1/4的概率选择,3/4的概率重投,如果是3-6,100%选择。这样
所有数字概率都一样。然后你需要一个东西能做1/4概率的,随便选三个1/12的数字加
起来就是了,所以整个过程是如果投到1和2,再投一次骰子,如果是3-5中的任意一个
就取之前的1或2,否则重投。
avatar
M*6
3
1和2都是投出4个才打印出来是不是就可以了? 其他的摇到一个就打印一个。。
用一个arr1来保存各个数字投出几次才算投了1次。另一个array count来计数保存当前
各个数字已投出的个数。
比如 输入时 int p{1/3, 1/5, 1/5, 1/3, 1/5};
那么,int[] arr1 = {5, 3, 3, 5, 3}
int[] count = new int[arr1.length]
int num = rollDice();
count[num]++;
if(count[num] == arr1[num]){
count[num] = 0;
println(num);
}
或者自己实现一个概率函数决定当前摇出的数字要不要用。产生一个0到1之
间的随机数,如果它小与选中的概率就return true,否则就return false。
avatar
k*a
4
第一次 3 or 4 ===> 3
5 or 6 ===> 5
1 then roll one more time: 1 or 3 or 4 ===> 1
2 or 5 or 6 ====> 4
2 then roll one more time: 1 or 3 or 4 ===> 2
2 or 5 or 6 ====> 6
avatar
m*d
5
如果给的是一个参数呢? 不是这个数字, 就是如果六面的概率分别是a1,a2,a3,a4,a5
,a6 那应该怎么处理?

【在 k*******a 的大作中提到】
: 第一次 3 or 4 ===> 3
: 5 or 6 ===> 5
: 1 then roll one more time: 1 or 3 or 4 ===> 1
: 2 or 5 or 6 ====> 4
: 2 then roll one more time: 1 or 3 or 4 ===> 2
: 2 or 5 or 6 ====> 6

avatar
g*2
6
use permutations (permutations are uniformly distributed).
here you can use permutations of 3 unique numbers, which have 6 patterns.
just let each pattern to represent one number.
avatar
w*x
7
想到一个方法不知道算不算耍赖。随机掷一个数,然后生成一个{0,1}^3的随机数,然
后拿点数去xor这个随机数。如果001到110就不重复以上过程,。那么现在问题就到了
一个生成PRG
上了,那样就有很多标准算法了。
avatar
x*a
8
还是二楼的答案最方便快速简洁易懂
avatar
e*s
9
没那么复杂 投2次就投了
第一次3个uniform 1/3
第二次2个uniform 1/2
e.g.
第一次
{1, 2, {3 or 4 or 5 or 6} }
第二次
{ {1 or 3 or 4}, {2 or 5 or 6} }

【在 x*****a 的大作中提到】
: 还是二楼的答案最方便快速简洁易懂
avatar
e*s
10
具体怎么生成呢 持续投直到生成一个permutation?
worst case可能需要投掷接近无限次 比如一个面的概率接近0.
当然感觉可以只取其中概率较大的那几个面.

【在 g******2 的大作中提到】
: use permutations (permutations are uniformly distributed).
: here you can use permutations of 3 unique numbers, which have 6 patterns.
: just let each pattern to represent one number.

avatar
g*d
11
为什么 总概率 大于1

4.

【在 m**********d 的大作中提到】
: 怎么样用一个不均匀的筛子。比如 1/3 概率是1,1/3概率是2, 1/12 是3 ,1/12 是4.
: 1/12 是5,1/12 是6, 掷出均匀的点数。
: 问题 用这个筛子写一个ramdon函数, 返回的数字1-6 是均匀出现的 。

avatar
f*m
12
投两次就可以
1, 1|3|4 => 1
1, 2|5|6 => 2
2, 1|3|4 => 3
2, 2|5|6 => 4
3|4, * => 5
5|6, * => 6
avatar
g*2
13
One way is to use one fixed set, say 1,2,3 and then use all permutations of
it. Every time you roll the die 3 times, and discard any other patterns.
A better way is to use all sets (6 choose 3 -> 20). Then you can create a
map of permutation to number (1-6). Also discard any other patterns.
A more efficient way might be to create uniform 0,1 function first. Here you
can also use permutation: roll a die twice, 0 if first roll < second roll
and 1 if first roll > second roll. discard ties.
Then it's just a problem of generating random numbers from uniform {0,1}
function, which is very simple (binary number generating).

【在 e*******s 的大作中提到】
: 具体怎么生成呢 持续投直到生成一个permutation?
: worst case可能需要投掷接近无限次 比如一个面的概率接近0.
: 当然感觉可以只取其中概率较大的那几个面.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。