Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 888f78f

Browse files
zhangzz2015gitbook-bot
authored andcommitted
GitBook: [greyireland#113] No subject
1 parent 3b9002a commit 888f78f

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

‎suan-fa-si-wei/sampling-fang-fa.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44

55
### **470.Implement Rand10() Using Rand7()**
66

7+
使用7个数字表达出更多个数字,构造两位7进制,(rand7() - 1) \* 7 + rand7() - 1,可以取得0-48之间的数字。如得到0-39之间的数字,使用(num % 10 + 1)等概率映射出1-10. 如得到40-48之间的数字,重新选择...直到选择到0-39之间的数字
8+
79
```cpp
810
// Some cc++
911
// transfer rand7 to rand10.
1012
class Solution {
1113
public:
12-
int rand10() {
13-
14-
// [1 7] (rand7()-1) * 7 + rand7() -1 [ 0 48] >=40. descard.
14+
int rand10() {
15+
// [1 7] (rand7()-1) * 7 + rand7() -1 [ 0 48] discard >=40.
16+
// obtain [0, 39] . %10 to get 0 - 9. +1 to get [1 10]
1517
int random = 49;
1618
while(random>=40)
1719
{

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /