D*n
2 楼
19.10 Write a method to generate a random number between 1 and 7, given
a method that generates a random number between 1 and 5 (i.e., implement
rand7() using rand5()).
看了书的答案还是不太明白啊。。。为什么rand2()用decision tree表示,到leaf会有
3的result??
answer: public static int rand7()
{
while(true)
{
int num = 5 * ( rand5() - 1) + ( rand5() - 1);
if ( num < 21 ) return ( num % 7 + 1);
}
}
哪位高手能解释一下这段代码的正确性??
a method that generates a random number between 1 and 5 (i.e., implement
rand7() using rand5()).
看了书的答案还是不太明白啊。。。为什么rand2()用decision tree表示,到leaf会有
3的result??
answer: public static int rand7()
{
while(true)
{
int num = 5 * ( rand5() - 1) + ( rand5() - 1);
if ( num < 21 ) return ( num % 7 + 1);
}
}
哪位高手能解释一下这段代码的正确性??
s*8
3 楼
s*n
4 楼
只用rand(5) -1, 产生0 5 10 15 20 25.需要再用一次生成01234
然后
0 + 01234
5 + 01234
10 +....
.....
就是0..25 的random distribution了。
剩下的就很好理解了。
given
【在 D***n 的大作中提到】![](/moin_static193/solenoid/img/up.png)
: 19.10 Write a method to generate a random number between 1 and 7, given
: a method that generates a random number between 1 and 5 (i.e., implement
: rand7() using rand5()).
: 看了书的答案还是不太明白啊。。。为什么rand2()用decision tree表示,到leaf会有
: 3的result??
: answer: public static int rand7()
: {
: while(true)
: {
: int num = 5 * ( rand5() - 1) + ( rand5() - 1);
然后
0 + 01234
5 + 01234
10 +....
.....
就是0..25 的random distribution了。
剩下的就很好理解了。
given
【在 D***n 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: 19.10 Write a method to generate a random number between 1 and 7, given
: a method that generates a random number between 1 and 5 (i.e., implement
: rand7() using rand5()).
: 看了书的答案还是不太明白啊。。。为什么rand2()用decision tree表示,到leaf会有
: 3的result??
: answer: public static int rand7()
: {
: while(true)
: {
: int num = 5 * ( rand5() - 1) + ( rand5() - 1);
m*u
5 楼
才便宜5块钱啊
【在 s****8 的大作中提到】![](/moin_static193/solenoid/img/up.png)
: 黑色周五去抢吧:
: http://www.huarenstore.com/2014thanks
【在 s****8 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: 黑色周五去抢吧:
: http://www.huarenstore.com/2014thanks
s*x
6 楼
1 <= rand5() <= 5
0 <= rand5() -1 <= 4
0 <= 5 *(rand5() -1) <= 20
1 <= 5 *(rand5() -1) + ( rand5() - 1) <= 25
0 <= rand5() -1 <= 4
0 <= 5 *(rand5() -1) <= 20
1 <= 5 *(rand5() -1) + ( rand5() - 1) <= 25
z*g
7 楼
买了两台,便宜了$20
S*i
9 楼
这款不错,我家就用这款,很实用的,感恩节买成83刀,店家还送了赠品:
http://www.360videoshopping.com/cn/joyoung-cts-1078s
【在 m*******u 的大作中提到】![](/moin_static193/solenoid/img/up.png)
: 以前有一个,坏掉了,准备再搞一台
: 不用太多功能,能做豆浆和米浆就行
http://www.360videoshopping.com/cn/joyoung-cts-1078s
【在 m*******u 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: 以前有一个,坏掉了,准备再搞一台
: 不用太多功能,能做豆浆和米浆就行
s*y
10 楼
I do not think it is right one.
I think this one is right:
http://stackoverflow.com/questions/137783/expand-a-random-range
1-7
int i;
do
{
i = 5 * (rand5() - 1) + rand5(); // i is now uniformly random between 1 a
nd 25
} while(i > 21);
// i is now uniformly random between 1 and 21
return i % 7 + 1; // result is now uniformly random between 1 and 7
【在 d**e 的大作中提到】![](/moin_static193/solenoid/img/up.png)
: 你这个没解释清楚为什么要这样做,上面slimcan说得比较明白。
I think this one is right:
http://stackoverflow.com/questions/137783/expand-a-random-range
1-7
int i;
do
{
i = 5 * (rand5() - 1) + rand5(); // i is now uniformly random between 1 a
nd 25
} while(i > 21);
// i is now uniformly random between 1 and 21
return i % 7 + 1; // result is now uniformly random between 1 and 7
【在 d**e 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: 你这个没解释清楚为什么要这样做,上面slimcan说得比较明白。
m*r
11 楼
西版的团购
豆浆机coupon:scgroup7 off$13
链接: http://www.huarenstore.com/joyoung-cts-1078s.html
订购。
团购说明:
1、coupon有效期为3天;
2、组员可以各自使用coupon下单,免税免邮资;
3、团购价为最优惠价,不能叠加使用其他coupon和获得评论返现。
【在 m*******u 的大作中提到】![](/moin_static193/solenoid/img/up.png)
: 才便宜5块钱啊
豆浆机coupon:scgroup7 off$13
链接: http://www.huarenstore.com/joyoung-cts-1078s.html
订购。
团购说明:
1、coupon有效期为3天;
2、组员可以各自使用coupon下单,免税免邮资;
3、团购价为最优惠价,不能叠加使用其他coupon和获得评论返现。
【在 m*******u 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: 才便宜5块钱啊
s*y
12 楼
also here:
http://www.mytechinterviews.com/equal-probability-between-1-and
int rand7() {
while (1) {
int num = 5*(rand5()-1) + rand5();
if (num < 22) return ((num % 7) + 1);
}
}
a
【在 s*****y 的大作中提到】![](/moin_static193/solenoid/img/up.png)
: I do not think it is right one.
: I think this one is right:
: http://stackoverflow.com/questions/137783/expand-a-random-range
: 1-7
: int i;
: do
: {
: i = 5 * (rand5() - 1) + rand5(); // i is now uniformly random between 1 a
: nd 25
: } while(i > 21);
http://www.mytechinterviews.com/equal-probability-between-1-and
int rand7() {
while (1) {
int num = 5*(rand5()-1) + rand5();
if (num < 22) return ((num % 7) + 1);
}
}
a
【在 s*****y 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: I do not think it is right one.
: I think this one is right:
: http://stackoverflow.com/questions/137783/expand-a-random-range
: 1-7
: int i;
: do
: {
: i = 5 * (rand5() - 1) + rand5(); // i is now uniformly random between 1 a
: nd 25
: } while(i > 21);
m*u
13 楼
我买了那个红色的欧克,因为觉得它比较漂亮
d*e
14 楼
区别不是很大吧
只是一个算1-25,一个算0-24
【在 s*****y 的大作中提到】![](/moin_static193/solenoid/img/up.png)
: also here:
: http://www.mytechinterviews.com/equal-probability-between-1-and
: int rand7() {
: while (1) {
: int num = 5*(rand5()-1) + rand5();
: if (num < 22) return ((num % 7) + 1);
: }
: }
:
: a
只是一个算1-25,一个算0-24
【在 s*****y 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: also here:
: http://www.mytechinterviews.com/equal-probability-between-1-and
: int rand7() {
: while (1) {
: int num = 5*(rand5()-1) + rand5();
: if (num < 22) return ((num % 7) + 1);
: }
: }
:
: a
D*n
16 楼
先谢谢各位大神啦。。。
答案待俺慢慢消化。。。
答案待俺慢慢消化。。。
c*p
21 楼
好像还有更给力的版本。。
目前这个版本,存在一直出大于22的数的可能。。。。
given
【在 D***n 的大作中提到】![](/moin_static193/solenoid/img/up.png)
: 19.10 Write a method to generate a random number between 1 and 7, given
: a method that generates a random number between 1 and 5 (i.e., implement
: rand7() using rand5()).
: 看了书的答案还是不太明白啊。。。为什么rand2()用decision tree表示,到leaf会有
: 3的result??
: answer: public static int rand7()
: {
: while(true)
: {
: int num = 5 * ( rand5() - 1) + ( rand5() - 1);
目前这个版本,存在一直出大于22的数的可能。。。。
given
【在 D***n 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: 19.10 Write a method to generate a random number between 1 and 7, given
: a method that generates a random number between 1 and 5 (i.e., implement
: rand7() using rand5()).
: 看了书的答案还是不太明白啊。。。为什么rand2()用decision tree表示,到leaf会有
: 3的result??
: answer: public static int rand7()
: {
: while(true)
: {
: int num = 5 * ( rand5() - 1) + ( rand5() - 1);
相关阅读
请教一道面试题目刚刚换了工作,就排期大跃进,perm现在要多久 (转载)11月22日提交H1B的TX有收到退回的check了么?收到OFFER,纠结于去工作还是读PHD谁听知道这个公司么?excelacomH1B Renew去ONSITE的话,机票时间能长点吗没有汇报employment给ISS,已经毕业快7个月了H1B PP RFE的receipt date疑问面试前和reception确认时间是不是很傻?没H1b解决之道Positions: Statistics, Econometrics, or Economics/Applied Mathematics (代人发)Job Opening: ERP engineer in Washington D.C.三个月的OFFER可以接吗?Job opportunity_plant breeding<求内退>求Facebook, Twitter, Amazon, Apple, MS等公司内推中国美国offer都有,打算回去 (转载)Offer 的几个问题Design Pattern: Factory method vs Abstract factory求指路人!!做研究的如何转向公司?