当前位置:网站首页>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
边栏推荐
- Apache MINA框架「建议收藏」
- wallys//wifi6 wifi5 router IPQ6018 IPQ4019 IPQ4029 802.11ax 802.11ac
- 【云原生 | 从零开始学Kubernetes】八、命名空间资源配额以及标签
- 【高等数学】【3】微分中值定理与导数的应用
- PMP每日一练 | 考试不迷路-7.25
- 【TensorRT】动态batch进行推理
- JS scope and scope chain
- 2022.7.24-----leetcode.1184
- CarSim simulation quick start (XIV) - CarSim Simulink joint simulation
- Behind every piece of information you collect, you can't live without TA
猜你喜欢

Do you still have certificates to participate in the open source community?

4. Server startup of source code analysis of Nacos configuration center

网络协议:TCP Part2

Share 25 useful JS single line codes

Fanoutexchange switch code tutorial

PMP practice once a day | don't get lost in the exam -7.25

Socket error Event: 32 Error: 10053. Connection closing...Socket close
![[advanced mathematics] [6] differential calculus of multivariate functions](/img/9e/84fe6f74b58cbaabab1b6eed0df556.png)
[advanced mathematics] [6] differential calculus of multivariate functions
![[today in history] July 4: the first e-book came out; The inventor of magnetic stripe card was born; Palm computer pioneer was born](/img/0b/73f0d98a6db813e54074abe199ed98.png)
[today in history] July 4: the first e-book came out; The inventor of magnetic stripe card was born; Palm computer pioneer was born
![[today in history] June 28: musk was born; Microsoft launched office 365; The inventor of Chua's circuit was born](/img/bf/09ccf36caec099098a22f0e8b670bd.png)
[today in history] June 28: musk was born; Microsoft launched office 365; The inventor of Chua's circuit was born
随机推荐
Clickhouse notes 02 -- installation test clickvisual
CarSim simulation quick start (XV) - ADAS sensor objects of CarSim sensor simulation (1)
Application of conductive slip ring in mechanical equipment
Log in to Baidu online disk with cookies (websites use cookies)
Go language go language built-in container
网络爬虫原理解析「建议收藏」
Aircraft PID control (rotor flight control)
Advantages of network virtualization of various manufacturers
QQ是32位还是64位软件(在哪看电脑是32位还是64位)
Myormframeworkjdbc review and problem analysis of user-defined persistence layer framework, and thought analysis of user-defined persistence layer framework
导电滑环在机械设备方面的应用
CarSim simulation quick start (XIV) - CarSim Simulink joint simulation
9.< tag-动态规划和子序列, 子数组>lt.718. 最长重复子数组 + lt.1143. 最长公共子序列
JS作用域与作用域链
4everland storage node portal network design
Export and call of onnx file of pytorch model
redis源码 -ziplist
Difference Between Accuracy and Precision
【云原生 | 从零开始学Kubernetes】八、命名空间资源配额以及标签
CarSim仿真快速入门(十四)—CarSim-Simulink联合仿真