c***2 发帖数: 838 | 1 where are the basics/algorithms to solve random number problems?
for example,
you have a dice to generate random numbers 1-6
how to generate 1-5 or 1-8?
Thanks, |
f*****w 发帖数: 2602 | |
s******o 发帖数: 2233 | 3 suppose r() genereates 1-6
then (r()-1)/5*4+1 generates 1-5
【在 c***2 的大作中提到】 : where are the basics/algorithms to solve random number problems? : for example, : you have a dice to generate random numbers 1-6 : how to generate 1-5 or 1-8? : Thanks,
|
c***2 发帖数: 838 | 4 is it okay doing this to get 1-5:
do {
num=r();
} while (num==6);
return num; |
g*********s 发帖数: 1782 | 5 yes.
【在 c***2 的大作中提到】 : is it okay doing this to get 1-5: : do { : num=r(); : } while (num==6); : return num;
|
P********l 发帖数: 452 | 6 http://www.sureinterview.com/shwqst/125002
http://www.sureinterview.com/schlsc?q=random
从rand6到rand8很简单。因为rand8的因子可以从rand6得到.
rand8 = (((rand6-1)*6 + (rand6-1))*6 + (rand6-1)) % 8 + 1.
This is an old question.
【在 c***2 的大作中提到】 : where are the basics/algorithms to solve random number problems? : for example, : you have a dice to generate random numbers 1-6 : how to generate 1-5 or 1-8? : Thanks,
|
g*********s 发帖数: 1782 | 7 或者生成1~4,再生成1~2,相乘。
【在 P********l 的大作中提到】 : http://www.sureinterview.com/shwqst/125002 : http://www.sureinterview.com/schlsc?q=random : 从rand6到rand8很简单。因为rand8的因子可以从rand6得到. : rand8 = (((rand6-1)*6 + (rand6-1))*6 + (rand6-1)) % 8 + 1. : This is an old question.
|
p******r 发帖数: 2999 | 8 how do you get 5?
【在 g*********s 的大作中提到】 : 或者生成1~4,再生成1~2,相乘。
|
x*****p 发帖数: 1707 | 9 The basic idea is like this.
If n>m and gcd(n,m) = 1, from rand_n to generate rand_m, we just do rand_n
and skip those who is greater than m. Then 1, ..., m is evenly distributed,
but the total probability is less than 1.
If n
numbers, let k be the greatest int such that km
greater than km and it is done. |
l*****a 发帖数: 14598 | 10 you can't do like this
there are 1/6 probability to get one of 1,2,3,4,5,6
then also 1/6 for r()-1 as 0,1,2,3,4,5
so the probability of each of below is also 1/6
0/5*4+1=1
1/5*4+1
2/5*4+1
3/5*4+1
4/5*4+1
5/5*4+1=5
BTW,for the middle four,i assume that they are all 1 for integer.
since 1/5 should be 0.
【在 s******o 的大作中提到】 : suppose r() genereates 1-6 : then (r()-1)/5*4+1 generates 1-5
|
l*****a 发帖数: 14598 | 11 my solution to this is
while(1)
{
sum=(random6()-1)*6+(random6()-1);//sum is 0..35
if(sum==35) continue;
else return sum%5;
}
【在 l*****a 的大作中提到】 : you can't do like this : there are 1/6 probability to get one of 1,2,3,4,5,6 : then also 1/6 for r()-1 as 0,1,2,3,4,5 : so the probability of each of below is also 1/6 : 0/5*4+1=1 : 1/5*4+1 : 2/5*4+1 : 3/5*4+1 : 4/5*4+1 : 5/5*4+1=5
|
f*****w 发帖数: 2602 | 12
你这个0..35不是uniform的
【在 l*****a 的大作中提到】 : my solution to this is : while(1) : { : sum=(random6()-1)*6+(random6()-1);//sum is 0..35 : if(sum==35) continue; : else return sum%5; : }
|
a******e 发帖数: 95 | 13 Think one more time and you'll know it is.
Hint: it uses base 6 representation.
【在 f*****w 的大作中提到】 : : 你这个0..35不是uniform的
|