当前位置:网站首页>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
边栏推荐
- [today in history] July 18: Intel was founded; The first photo was posted on the world wide web; EBay spins off PayPal
- Redis source code -ziplist
- 9.< tag-动态规划和子序列, 子数组>lt.718. 最长重复子数组 + lt.1143. 最长公共子序列
- Arrow parquet
- 10.< tag-动态规划和子序列, 子数组>lt.53. 最大子数组和 + lt.392. 判断子序列 dbc
- 【TensorRT】trtexec工具转engine
- Arrow 之 Parquet
- Google pixel 6A off screen fingerprint scanner has major security vulnerabilities
- tga文件格式(波形声音文件格式)
- "Chain" connects infinite possibilities: digital asset chain, wonderful coming soon!
猜你喜欢

【TensorRT】动态batch进行推理

【高等数学】【5】定积分及应用

Clickhouse notes 02 -- installation test clickvisual

增加 swap 空间

Advantages of network virtualization of various manufacturers

Vivo official website app full model UI adaptation scheme

Timing analysis and constraints based on xlinx (1) -- what is timing analysis? What are temporal constraints? What is temporal convergence?

Prescan quick start to master Lesson 19: prescan actuator configuration, track synchronization and non configuration of multiple tracks
![[today in history] July 3: ergonomic standards act; The birth of pioneers in the field of consumer electronics; Ubisoft releases uplay](/img/18/b06e2e5a2f76dc2da1c2374b8424b3.png)
[today in history] July 3: ergonomic standards act; The birth of pioneers in the field of consumer electronics; Ubisoft releases uplay

Share 25 useful JS single line codes
随机推荐
各厂商网络虚拟化的优势
“链”接无限可能:数字资产链,精彩马上来!
Web crawler principle analysis "suggestions collection"
"Chain" connects infinite possibilities: digital asset chain, wonderful coming soon!
[advanced mathematics] [5] definite integral and its application
Kubernetes进阶部分学习笔记
Recommendations on how to install plug-ins and baby plug-ins in idea
4. Server startup of source code analysis of Nacos configuration center
wallys//IPQ5018/IPQ6010/PD-60 802.3AT Input Output 10/100/1000M
RF, gbdt, xgboost feature selection methods "recommended collection"
JVM (XXIII) -- JVM runtime parameters
How to ensure the quality of customized slip rings
Docker 搭建 Redis Cluster集群
TGA file format (waveform sound file format)
Summarize the level of intelligent manufacturing discussion [macro understanding]
[today in history] June 29: SGI and MIPS merged; Microsoft acquires PowerPoint developer; News corporation sells MySpace
【ONNX】pytorch模型导出成ONNX格式:支持多参数与动态输入
Arrow parquet
Recommended system topic | Minet: cross domain CTR prediction
How much memory does bitmap occupy in the development of IM instant messaging?