Careercup Nice Question

出自LajiWiki
跳转到: 导航, 搜索

目录

随机数5->7

Given a function randfive() that generates a number between 1 to 5, with uniform probability. 
Use this to write a function randseven() which generates a number with uniform probability between 1 to 7.

1.run randfive() twice to generate two numbers a and b.
2.generate a new number c = (a-1)*5+(b-1) (24>=c>=0)
3.if 24>=c>=21, go back to step 1.
4.else return c%7+1 as the random number.


1. using randfive write a function (rand_0_or_1) to generate no 0 or 1 with uniform probablity
2. use rand_0_or_1 in randseven to generate no between 1 to 7 with uniform probability
Hint - call rand_0_or_1 3 times.


个人工具