当前位置:网站首页>Leetcode-470. implement rand10() with rand7()
Leetcode-470. implement rand10() with rand7()
2022-07-27 22:50:00 【KGundam】
Mathematical problems
Topic details
Given method rand7 Generative [1,7] Uniform random integer in range , Try to write a method rand10 Generate [1,10] Uniform random integer in range .
You can only call rand7() And you can't call other methods . Please do not use the system's Math.random() Method .
Each test case will have an internal parameter n, That is, the function you implement rand10() The number of times it will be called during the test . Please note that , This is not passed on to rand10() Parameters of .
Example 1:
Input : 1
Output : [2]
Example 2:
Input : 2
Output : [2,8]
Example 3:
Input : 3
Output : [3,8,10]
Ideas :
Let's think about it first , Use rand2() Can generate [1,2] The random number , So how to get [1,4] Well ?
Do you want to use it at once rand2()+rand2() Well ?
rand2() + rand2() = ? ==> [2,4]
1 + 1 = 2
1 + 2 = 3
2 + 1 = 3
2 + 2 = 4
In order to narrow the range of generating random numbers to [1,n], So after the result of the previous step, subtract 1
(rand2()-1) + rand2() = ? ==> [1,3]
0 + 1 = 1
0 + 2 = 2
1 + 1 = 2
1 + 2 = 3
You can see , Although the result is [1,3] But the probability is not equal , So it can't be used
Then if we put (rand()2-1)×2 Well ?( How did you think of me here TM I don't know. ) We found that
(rand2()-1) × 2 + rand2() = ? ==> [1,4]
0 + 1 = 1
0 + 2 = 2
2 + 1 = 3
2 + 2 = 4
The probability is exactly equal , We have passed rand2() Realized rand4()
So we come to a mathematical law :
It is known that rand_N() Can be generated with equal probability [1, N] Random number of ranges
that :
(rand_X() - 1) × Y + rand_Y() ==> Can be generated with equal probability [1, X * Y] Random number of ranges
It is realized. rand_XY()
Let's think about another question : How to pass in reverse rand4() Realization rand2() Well ? We can achieve by taking the remainder
rand4() % 2 + 1 = ?
1 % 2 + 1 = 2
2 % 2 + 1 = 1
3 % 2 + 1 = 2
4 % 2 + 1 = 1
in fact , as long as N yes 2 Multiple , We can all go through randN() To achieve rand2(), It's equal probability !
On the contrary, if N No 2 You cannot get equal probability by taking the remainder of the multiple of , Such as :
rand6() % 2 + 1 = ?
1 % 2 + 1 = 2
2 % 2 + 1 = 1
3 % 2 + 1 = 2
4 % 2 + 1 = 1
5 % 2 + 1 = 2
6 % 2 + 1 = 1
rand5() % 2 + 1 = ?
1 % 2 + 1 = 2
2 % 2 + 1 = 1
3 % 2 + 1 = 2
4 % 2 + 1 = 1
5 % 2 + 1 = 2
With the above regular method , We can analyze the problem , We need to get through rand7() Realization rand10()
Then we can realize randN()–N by 10 Multiple , Then take the rest
We deal with it according to the above rules :
(rand7()-1) × 7 + rand7() ==> rand49()
however 49 No 10 Multiple of ! We can use " Reject sampling "( Reject any sampling result you don't want )
hold rand49() Produced 41~49 Just refuse all and take the rest …
Code :
// The rand7() API is already defined for you.
// int rand7();
// @return a random integer in the range 1 to 7
class Solution
{
public:
int rand10()
{
while (true)
{
int num = (rand7() - 1) * 7 + rand7(); // obtain [1,49]
if (num <= 40) // Reject sampling , obtain [1,40]
return num % 10 + 1; // Get surplus [1,10]
}
}
};
Optimize
We are in the above method ," Reject sampling " 了 41-49, Then can we make rational use of these nine numbers , So as to improve the efficiency of generating random numbers , We can use 41-49 subtract 40 obtain rand9() Further get rand10(), At this time, we found that after processing again " Refuse "61-63 Three random numbers , We can use it to subtract 60 obtain rand3() Further get rand10(), At this time, the last one " Refuse " 了 21 a number , We don't need to deal with , It's over , Improves the hit rate of random numbers
// The rand7() API is already defined for you.
// int rand7();
// @return a random integer in the range 1 to 7
class Solution
{
public:
int rand10()
{
while (true)
{
int a = rand7();
int b = rand7();
int num = (a - 1) * 7 + b; // rand49()
if (num <= 40) return num % 10 + 1; // Refuse sampling and get rand10()
// The following code deals with the useless random numbers obtained above [41,49]
a = num - 40; // rand49()-rand40()=rand9()
b = rand7();
num = (a - 1) * 7 + b; // rand63
if (num <= 60) return num % 10 + 1; // Refuse sampling and get rand10()
// The following code deals with the useless random numbers obtained above [61,63]
a = num - 60; // rand3
b = rand7();
num = (a - 1) * 7 + b; // rand21
if (num <= 20) return num % 10 + 1; // Refuse sampling and get rand10()
// Further down, you can no longer make the best use of everything , Because there is only one left 21 了
}
}
};
边栏推荐
- electromagnetic relay
- The execution process, orphan process and zombie process of fork() function
- cache学习
- [NOI2018] 冒泡排序(组合+卡特兰数+dp+树状数组)
- [SQL] SQL optimization
- US officials suggested trump prevent Infineon from acquiring cypress
- Relationship between DBM and VPP and Vpeak
- MeterSphere金融公司落地经验分享
- 物联网架构完全指南
- 2022/5/17考试总结
猜你喜欢
![The wave of smart home is coming, how to make machines understand the world [there is information at the end]](/img/8a/533e7f1fc96c03e6f8140efdd17983.png)
The wave of smart home is coming, how to make machines understand the world [there is information at the end]
![[untitled]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[untitled]

BUUCTF刷题十一道(05)

electromagnetic relay

Relationship between DBM and VPP and Vpeak

iptables学习

Leetcode15 -- sum of three numbers

基于MCU的二维码生成及在墨水屏上进行二维码显示

PyQt5快速开发与实战 4.9 对话框类控件

Chrome realizes automated testing: recording and playback web page actions
随机推荐
MeterSphere金融公司落地经验分享
android 11 安全策略及权限管理
Leetcode383 ransom letter
[untitled]
[OBS] P B frame loss threshold buffer_ duration_ usec
Window localstorage properties and location objects
Multi tenant SaaS cloud platform framework
It is said that Huawei will cut the order again! Supply chain manufacturers are more difficult
紫光FPGA解决口罩难题!助力口罩生产全面提速
[binary tree] count the number of good nodes in the binary tree
Alibaba Senior Software Testing Engineer recommends testers to learn -- Introduction to security testing
Two dimensional code generation based on MCU and two dimensional code display on ink screen
解决ip地址访问末位奇数通偶数不通,或者偶数通奇数不通的问题(云加密机连接云服务器时遇到的问题,全程记录,希望能给大佬们灵感)
Redis learning
Cache learning
SQL injection less26a (Boolean blind injection)
Can uniswap integrate sudoswap to open a new prelude to NFT liquidity?
setContentView详解
【图解】三次握手,四次挥手 —— 用心看这一篇就够了
CMOS switch (II)_ Parameter extraction