码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-02-01 15:27:10      阅读:236      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!