首页 > 其他 > 详细

zenefits oa - random(5) to generate a random(7)

时间:2016-02-01 15:25:06      阅读:162      评论:0      收藏:0      [点我收藏+]

If given a function that generates a random number from 1 to 5, how do you use this function to generate a random number from 1 to 7 with the same probability? (ie. function a has probability 1/5 per number, function b would have probability 1/7).  

 

int sum = 0;
int times = 5;
while(times>0){
  times--;
  sum += rand(5);
}

// 5 ~ 25  = 3 *7
return (sum - 2) / 3;

zenefits oa - random(5) to generate a random(7)

原文:http://www.cnblogs.com/marc-leetcode/p/5175013.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!