当前位置:网站首页>Rand1 generates rand9
Rand1 generates rand9
2022-07-25 20:24:00 【Love cat big carp】
List of articles
1. Problem description
This is a piece of me in 20220706 Wednesday morning at Shenzhen Science Park D1 Algorithm problems encountered in participating in futu social recruitment .
Given a function rand1 Meeting 50% The probability output of 0 and 1, Please use rand1 Realization rand9, Output with equal probability 0~9 this 10 A digital .
2. Difficulty level
The difficulty should be middle.
3. Solutions
From the point of view of the subject , There is a known condition ,rand1() Meeting 50% The probability output of 0 and 1, Implement a new function on the basis of known conditions rand9(), So it's equivalent to based on 50% The probability generator creates a 10% Probability generator .
How to create a 10% What about the event generator , We know from the formula ,rand1 Probability of occurrence P(0)=50%,P(1)=50%, that rand1 Perform twice , That is, both results are 1 The probability of is 25%, Three times it turned out to be 1 The probability of is 12.5%, All four executions are 1 The probability of is 6.25%, Equivalent to four times 1111 The probability of combination is 6.25%, That is to say 1/16.
Is this data familiar ? We use the program to generate it four times 0 and 1 The number of combinations produced :
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
It's a binary number , After converting to decimal , Namely 0~15, amount to 16 The probability of occurrence of each number is 1/16; in other words , produce 0 To 9 The probability is equal , Although the probability is not 1/10.
So the solution of this problem is to reject the sampling method : call 4 Time rand1, Generate 4 The binary number of bits , And then convert it to 10 Hexadecimal number , If this number is greater than 9, Then regenerate .
4. Implementation example
Use Golang Give an implementation example .
First of all, we realize rand1.
package main
import (
"math/rand"
)
// rand1 Equal probability output 0 and 1.
func rand1() int {
return rand.Intn(2)
}
According to rand1 Realization rand9:
// rand9 Equal probability output 0 ~ 9.
func rand9() int {
for {
n := rand1()<<3 + rand1()<<2 + rand1()<<1 + rand1()
if n < 10 {
return n
}
}
}
rand9 Output example :
1
4
8
6
9
3
2
2
8
5
reference
It is known that f(x) Incoming value Equal probability Output 0 or 1, If you write one f1(x) Achieve equal probability output 0-9?| segmentfault
use Rand7() Realization Rand10() | LeetCode
边栏推荐
- [advanced mathematics] [1] function, limit, continuity
- "Chain" connects infinite possibilities: digital asset chain, wonderful coming soon!
- [today in history] July 18: Intel was founded; The first photo was posted on the world wide web; EBay spins off PayPal
- 【高等数学】【4】不定积分
- CarSim simulation quick start (XIV) - CarSim Simulink joint simulation
- 网络RTK无人机上机测试[通俗易懂]
- String of sword finger offer question bank summary (II) (C language version)
- securecrt乱码解决方法[通俗易懂]
- Fanoutexchange switch code tutorial
- 网络爬虫原理解析「建议收藏」
猜你喜欢

wallys//IPQ5018/IPQ6010/PD-60 802.3AT Input Output 10/100/1000M

Prescan quick start to master the special functions of prescan track editing in lecture 18
![[today in history] July 17: Softbank acquired arm; The first email interruption; Wikimedia International Conference](/img/0f/8ce2d5487b16d38a152cfd3ab454bb.png)
[today in history] July 17: Softbank acquired arm; The first email interruption; Wikimedia International Conference

Recommendations on how to install plug-ins and baby plug-ins in idea

Export and call of onnx file of pytorch model

各厂商网络虚拟化的优势

Recommended system topic | Minet: cross domain CTR prediction

Increase swap space

Docker builds redis cluster

9. < tag dynamic programming and subsequence, subarray> lt.718. Longest repeated subarray + lt.1143. Longest common subsequence
随机推荐
Array of sword finger offer question bank summary (I) (C language version)
Cloud native guide: what is cloud native infrastructure
TGA file format (waveform sound file format)
飞行器pid控制(旋翼飞控)
[cloud native | learn kubernetes from scratch] VIII. Namespace resource quotas and labels
CarSim simulation quick start (XV) - ADAS sensor objects of CarSim sensor simulation (1)
Chapter VI modified specification (SPEC) class
How to ensure the quality of customized slip rings
String of sword finger offer question bank summary (II) (C language version)
redis源码 -ziplist
Link list of sword finger offer question bank summary (III) (C language version)
谷歌Pixel 6a屏下指纹扫描仪存在重大安全漏洞
Technology cloud report: what is the difference between zero trust and SASE? The answer is not really important
[Infographics Show] 248 Public Domain Name
Aircraft PID control (rotor flight control)
Export and call of onnx file of pytorch model
[today in history] July 1: the father of time-sharing system was born; Alipay launched barcode payment; The first TV advertisement in the world
Proxy实现mysql读写分离
Principle analysis of bootloader
Timing analysis and constraints based on xlinx (1) -- what is timing analysis? What are temporal constraints? What is temporal convergence?