【LeetCode】470. Implement Rand10() Using Rand7() 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/implement-rand10-using-rand7/description/
题目描述
Given a function rand7 which generates a uniform random integer in the range 1 to 7, write a function rand10 which generates a uniform random integer in the range 1 to 10.
Do NOT use system’s Math.random().
Example 1:
Input: 1
Output: [7]
Example 2:
Input: 2
Output: [8,4]
Example 3:
Input: 3
Output: [8,1,10]
Note:
- rand7 is predefined.
- Each testcase has one argument: n, the number of times that rand10 is called.
Follow up:
- What is the expected value for the number of calls to rand7() function?
- Could you minimize the number of calls to rand7()?
题目大意
利用一个范围在1~7的随机数产生器,构造一个范围在1~10的随机数产生器。
解题方法
范围在1~7的随机数产生器,即1~7各个数字出现的概率皆为1/7.
范围在1~10的随机数产生器,即1~10各个数字出现的概率皆为1/10.
这个题的构造思路是先构造一个randN,这个N必须是10的整数倍,然后randN % 10就可以得到了rand10.
所以可以从rand7先构造出rand49,再把rand49中大于等于40的都过滤掉,这样就得到了rand40,在对10取余即可。
具体一点就是:
- rand7()等概率地产生1,2,3,4,5,6,7.
- rand7() - 1等概率地产生[0,6].
- (rand7() - 1) *7等概率地产生0, 7, 14, 21, 28, 35, 42
- (rand7() - 1) * 7 + (rand7() - 1)等概率地产生[0, 48]这49个数字
- 如果步骤4的结果大于等于40,那么就重复步骤4,直到产生的数小于40
- 把步骤5的结果mod 10 再加1, 就会等概率的随机生成[1, 10]
所以过程是:
rand7 --> rand49 --> rand40 --> rand10.
其中,构造rand49的方式是:
7 * (rand7() - 1) + rand7() - 1
那么,如何理解呢?上面的做法到底如果理解才能记住呢?看下面这个表格:

看上面的表格,可以这么理解:我们先产生[1,49]之间的随机正整数,构建的方式是扔两次rand7()两次的结果分别作为表格的行和列,因此,7 * (rand7() - 1) + rand7()的取值范围是[1, 49],代表上面表格的任意位置。注意,我们需要把上图黄色部分的给过滤掉,因为这9个数字超出了40的范围,即如果两次扔rand7()产生的数字在上面黄色的部分需要重新扔。得到了[1,49]之后,然后减去1 再 mod 10 再加1即可。
这个方法我觉得最好要记住。
# The rand7() API is already defined for you.
# def rand7():
# @return a random integer in the range 1 to 7
class Solution:
def rand10(self):
"""
:rtype: int
"""
return self.rand40() % 10 + 1
def rand49(self):
"""
random integer in 0 ~ 48
"""
return 7 * (rand7() - 1) + rand7() - 1
def rand40(self):
"""
random integer in 0 ~ 40
"""
num = self.rand49()
while num >= 40:
num = self.rand49()
return num
C++代码如下:
// 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() {
return rand40() % 10 + 1;
}
int rand49() {
return (rand7() - 1) * 7 + (rand7() - 1);
}
int rand40() {
int r = rand49();
while (r >= 40) {
r = rand49();
}
return r;
}
};
日期
2018 年 8 月 18 日 —— 天在下雨
2019 年 1 月 23 日 —— 又一连过去了几天
【LeetCode】470. Implement Rand10() Using Rand7() 解题报告(Python & C++)的更多相关文章
- [LeetCode] 470. Implement Rand10() Using Rand7()
Given a function rand7 which generates a uniform random integer in the range 1 to 7, write a functio ...
- LC 470. Implement Rand10() Using Rand7()
Given a function rand7 which generates a uniform random integer in the range 1 to 7, write a functio ...
- 470. Implement Rand10() Using Rand7() (拒绝采样Reject Sampling)
1. 问题 已提供一个Rand7()的API可以随机生成1到7的数字,使用Rand7实现Rand10,Rand10可以随机生成1到10的数字. 2. 思路 简单说: (1)通过(Rand N - 1) ...
- 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- 【LeetCode】784. Letter Case Permutation 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 循环 日期 题目地址:https://leet ...
- 【LeetCode】731. My Calendar II 解题报告(Python)
[LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- 【LeetCode】732. My Calendar III解题报告
[LeetCode]732. My Calendar III解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/my-calendar ...
- 【LeetCode】729. My Calendar I 解题报告
[LeetCode]729. My Calendar I 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/my-calendar- ...
随机推荐
- Python序列化,json&pickle&shelve模块
1. 序列化说明 序列化可将非字符串的数据类型的数据进行存档,如字典.列表甚至是函数等等 反序列化,将通过序列化保存的文件内容反序列化即可得到数据原本的样子,可直接使用 2. Python中常用的序列 ...
- Spring Cloud 2021.0.0 正式发布,第一个支持Spring Boot 2.6的版本!
美国时间12月2日,Spring Cloud 正式发布了第一个支持 Spring Boot 2.6 的版本,版本号为:2021.0.0,codename 为 Jubilee. 在了解具体更新内容之前, ...
- DOM给表格添加新一行和删除整个行的内容
DOM用appendChild()给表格添加新一行时,要注意,在HTML中没特别设置<thead>,<tbody>时,会自动添加上,所以要选择表格第一个元素在添加tr. // ...
- HDFS06 DataNode
DataNode 目录 DataNode DataNode工作机制 数据完整性 DataNode掉线时限参数设置 DataNode工作机制 一个数据块在DataNode上以文字形式存储在磁盘上,包括一 ...
- 学习java 7.7
学习内容: 多态转型:向上转型 Animal a = new Cat(); a.eat(); 向下转型 Cat c = (Cat)a; c.eat(); 抽象方法没有方法体,抽象类中有抽象方法 抽象类 ...
- JavaScript 链表
------------恢复内容开始------------ 背景 数组并不总是组织数据的最佳数据结构,原因如下.在很多编程语言中,数组的长度是固定的,所以当数组已被数据填满时,再要加入新的元素就会非 ...
- IntentFilter,PendingIntent
1.当Intent在组件间传递时,组件如果想告知Android系统自己能够响应那些Intent,那么就需要用到IntentFilter对象. IntentFilter对象负责过滤掉组件无法响应和处理的 ...
- android:为TextView添加样式、跑马灯、TextSwitcher和ImageSwitcher实现平滑过渡
一.样式 设置下划线: textView.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);//下划线 textView.getPaint().setAnt ...
- Linux基础命令---mput上传ftp文件
mput 使用lftp登录ftp服务器之后,可以使用put指令将文件上传到服务器.mput指令可以使用通配符,而put指令则不可以. 1.语法 mput [-c] [-d] [-a] ...
- my38_MySQL事务知识点零记
从innodb中查看事务信息 show engine innodb status\G; ------------ TRANSACTIONS------------Trx id counter 3153 ...