当前位置:网站首页>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
边栏推荐
- Notes - record a cannotfinddatasourceexception: dynamic datasource can not find primary datasource problem solving
- 【高等数学】【6】多元函数微分学
- Distributed link logging minbox logging usage document
- Web crawler principle analysis "suggestions collection"
- Log in to Baidu online disk with cookies (websites use cookies)
- Chapter VI modified specification (SPEC) class
- 当AI邂逅生命健康,华为云为他们搭建三座桥
- Clickhouse notes 02 -- installation test clickvisual
- PMP采用最新考纲,这里有【敏捷项目管理】
- 如何保证定制滑环质量
猜你喜欢
![Summarize the level of intelligent manufacturing discussion [macro understanding]](/img/84/3addabdf857c562535a4085782d3e8.png)
Summarize the level of intelligent manufacturing discussion [macro understanding]

JVM (XXIII) -- JVM runtime parameters

笔记——记录一个CannotFindDataSourceException: dynamic-datasource can not find primary datasource问题解决

「分享」DevExpress ASP.NET v22.1最新版本系统环境配置要求

Difference Between Accuracy and Precision
![[today in history] June 29: SGI and MIPS merged; Microsoft acquires PowerPoint developer; News corporation sells MySpace](/img/86/abeb82927803712a98d2018421c3a7.png)
[today in history] June 29: SGI and MIPS merged; Microsoft acquires PowerPoint developer; News corporation sells MySpace

Share 25 useful JS single line codes

Recommended system topic | Minet: cross domain CTR prediction

Jmeter——接口测试

Increase swap space
随机推荐
PMP采用最新考纲,这里有【敏捷项目管理】
Difference Between Accuracy and Precision
[today in history] July 2: BitTorrent came out; The commercial system linspire was acquired; Sony deploys Playstation now
Log in to Baidu online disk with cookies (websites use cookies)
tga文件格式(波形声音文件格式)
接口请求合并的3种技巧,性能直接爆表!
Prescan quick start to master Lesson 19: prescan actuator configuration, track synchronization and non configuration of multiple tracks
Working principle of radar water level gauge and precautions for installation and maintenance
"Share" devaxpress asp Net v22.1 latest version system environment configuration requirements
【TensorRT】动态batch进行推理
Remote monitoring solution of intelligent electronic boundary stake Nature Reserve
9.< tag-动态规划和子序列, 子数组>lt.718. 最长重复子数组 + lt.1143. 最长公共子序列
Jmeter——接口测试
[tensorrt] trtexec tool to engine
[advanced mathematics] [6] differential calculus of multivariate functions
10. < tag dynamic programming and subsequence, subarray> lt.53. maximum subarray and + lt.392. Judge subsequence DBC
SecureCRT garbled code solution [easy to understand]
Export and call of onnx file of pytorch model
第六章 修改规范(SPEC)类
FanoutExchange交换机代码教程