b*******a 发帖数: 68 | 1 Given a function which produces a random integer in the range 1 to 5, write
a function which produces a random integer in the range 1 to 7. |
s******e 发帖数: 285 | 2 jobhunting版考古
write
【在 b*******a 的大作中提到】 : Given a function which produces a random integer in the range 1 to 5, write : a function which produces a random integer in the range 1 to 7.
|
z*****n 发帖数: 7639 | 3 int r5();
int rand7(){
return (int) (r5()+r5()+r5()+r5()+r5()+r5()+r5())/5;
}
write
【在 b*******a 的大作中提到】 : Given a function which produces a random integer in the range 1 to 5, write : a function which produces a random integer in the range 1 to 7.
|
b*******a 发帖数: 68 | 4 I know, but I just could not find it... :-(
【在 s******e 的大作中提到】 : jobhunting版考古 : : write
|
b*******a 发帖数: 68 | 5 JobHunting 2019 -- I am reading it....
2019 m ● [合集] 那个Google random generate 1-7的题怎么做啊(14.0k)
4/1326 heing
2006-11-11 mengt
01-08 23:14
【在 b*******a 的大作中提到】 : I know, but I just could not find it... :-(
|
j**********s 发帖数: 132 | 6 这样做是错滴,几个 uniform distribution 的 random variable 之和就不在是
uniform distribution 了。
【在 z*****n 的大作中提到】 : int r5(); : int rand7(){ : return (int) (r5()+r5()+r5()+r5()+r5()+r5()+r5())/5; : } : : write
|
a***n 发帖数: 404 | 7 (rnd(5)+rnd(5)+rnd(5)+rnd(5)+rnd(5)+rnd(5)+rnd(5)) mod 7 ?
【在 j**********s 的大作中提到】 : 这样做是错滴,几个 uniform distribution 的 random variable 之和就不在是 : uniform distribution 了。
|
z*****n 发帖数: 7639 | 8 Oh, thanks for pointing out.
I will think of it again.
【在 j**********s 的大作中提到】 : 这样做是错滴,几个 uniform distribution 的 random variable 之和就不在是 : uniform distribution 了。
|
z*****n 发帖数: 7639 | 9 this is the same...
【在 a***n 的大作中提到】 : (rnd(5)+rnd(5)+rnd(5)+rnd(5)+rnd(5)+rnd(5)+rnd(5)) mod 7 ?
|
y**q 发帖数: 246 | 10 为什么要加那么多次?而不是直接7*rnd(5)?
【在 z*****n 的大作中提到】 : this is the same...
|
|
|
a***n 发帖数: 404 | 11 请具体讲讲是个啥分布了? 。。。。 晕了
【在 j**********s 的大作中提到】 : 这样做是错滴,几个 uniform distribution 的 random variable 之和就不在是 : uniform distribution 了。
|
a***n 发帖数: 404 | 12 人家是 /5 ,我是 mod 7 这两个一样么?
【在 z*****n 的大作中提到】 : this is the same...
|
z*****n 发帖数: 7639 | 13 because you used the same Uniform distribution addition,
this part is critical.
【在 a***n 的大作中提到】 : 人家是 /5 ,我是 mod 7 这两个一样么?
|
z*****n 发帖数: 7639 | 14 two uniformly distributed variables added together,
say v=u1+u2, is not a uniform distribution variable,
in stead, it becomes a triangular distribution,
3 uniformly distributed variables added together will
produce a more complex distribution...
A simple example: u1 and u2 uniformly distributes in
1 and 2
u1 = u2 : 1(0.5) 2(0.5)
u1 + u2 : 2(0.25) 3(0.5) 4(0.25)
【在 a***n 的大作中提到】 : 请具体讲讲是个啥分布了? 。。。。 晕了
|
a***n 发帖数: 404 | 15 why the addition result is not uniform distribution? then what distribution?
...:(
【在 z*****n 的大作中提到】 : because you used the same Uniform distribution addition, : this part is critical.
|
z*****n 发帖数: 7639 | 16 see my previous post :)
distribution?
【在 a***n 的大作中提到】 : why the addition result is not uniform distribution? then what distribution? : ...:(
|
a***n 发帖数: 404 | 17 哦。多谢。我再想想看看有没有啥别的办法。。
【在 z*****n 的大作中提到】 : two uniformly distributed variables added together, : say v=u1+u2, is not a uniform distribution variable, : in stead, it becomes a triangular distribution, : 3 uniformly distributed variables added together will : produce a more complex distribution... : A simple example: u1 and u2 uniformly distributes in : 1 and 2 : u1 = u2 : 1(0.5) 2(0.5) : u1 + u2 : 2(0.25) 3(0.5) 4(0.25)
|
a***n 发帖数: 404 | 18 got it~~ :)
【在 z*****n 的大作中提到】 : see my previous post :) : : distribution?
|
z*****n 发帖数: 7639 | 19 Ok, got a stupid way but gives uniform
distribution in (1,8), then for rnd7(), follow
the same way:
int rnd7(){
int k;
do{
k=rnd8();
}while(k==8);
return k;
}
int rnd8(void){
int k,h;
do{
k=rnd5();
}while(k==5);
do{
h=rnd5();
}while(h>2);
if(h==1) return k;
else return k+4;
}
【在 z*****n 的大作中提到】 : Oh, thanks for pointing out. : I will think of it again.
|
z*****n 发帖数: 7639 | 20 here is the whole program with test code, my
result is (for 1 to 7)
143283
142313
143015
142959
142752
142532
143146
#include
#include
#include
#define rnd5() rand()%5+1
int rnd8(void);
void main(){
int a, b[5]={0,0,0,0,0}, c, d[8]={0,0,0,0,0,0,0,0};
srand(time(NULL));
for(a=0; a<100000; a++){
c=rnd5();
b[c-1]++;
}
for(a=0; a<5; a++) printf("%d\t", b[a]);
for(a=0; a<1000000; a++){
【在 z*****n 的大作中提到】 : Ok, got a stupid way but gives uniform : distribution in (1,8), then for rnd7(), follow : the same way: : int rnd7(){ : int k; : do{ : k=rnd8(); : }while(k==8); : return k; : }
|
a***n 发帖数: 404 | 21 sounds good!
【在 z*****n 的大作中提到】 : here is the whole program with test code, my : result is (for 1 to 7) : 143283 : 142313 : 143015 : 142959 : 142752 : 142532 : 143146 : #include
|