[LeetCode] 470. Implement Rand10() Using Rand7()

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 的rand7() 做一个随机生成 1-10的rand10()

随机生成的数只有整数,要得到随机的10,就得想办法去搞到一个10的倍数的随机数集

10的倍数的数字必须每个数都出现

7的倍数往上走就会出现

{1,2,....,7}

{2,4,....,14}

{3,6,....,21}

......等等类似的序列,这些序列的特点就是中间会产生差值,rand7()会生成1-7的随机数

比方说 1+ rand7() 就不会产生1,

{7,14,...,49}因为必须要有前面的数字,那么,我们改为

{0,6,12,...,42},然后我们把每个数字加0-6,也就是rand7() - 1,就可以随机的得到0-48;

回到开头,我们需要满足某个10的整数倍内产生的 数是随机的,0-48是随机产生的,等概率

那么 0-39 这四十个数字出现概率是等概率的,我们舍弃40-48的数字,并不会产生影响,因为我们产生的其他数字是等概率的,我们只要那40个数字

最后由于0-39 对10取余没办法产生10,那么对其+1即可

class Solution extends SolBase {
    public int rand10() {
        int res = 40;
        while (res >= 40) {
            res = 7*(rand7()-1) + (rand7() - 1);
        }
        return res%10 + 1;
    }
}

原文地址:https://www.cnblogs.com/Moriarty-cx/p/9655067.html

时间: 2024-07-30 12:01:18

[LeetCode] 470. Implement Rand10() Using Rand7()的相关文章

LC 470. Implement Rand10() Using Rand7()

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:

470. Implement Rand10() Using Rand7() (拒绝采样Reject Sampling)

问题 已提供一个Rand7()的API可以随机生成1到7的数字,使用Rand7实现Rand10,Rand10可以随机生成1到10的数字. 思路 简单说: (1)通过(Rand N - 1) % 10 + 1的方法,可以求出Rand10,当N是10的倍数的时候. (2)用( Rand7 - 1 ) * 7 + Rand7可以随机生成1-49,记作Rand49. (3)如果可以通过Rand49计算出Rand40,即随机生成1-40,就可以通过Rand40 % 10来取得Rand10. (4)如何通过

[LeetCode] Implement Rand10() Using Rand7()

题目描述 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: 1Output: [7] Example

LeetCode470 - Implement Rand10() Using Rand7() - Medium (Python)

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:

[LeetCode] Implement Rand10() Using Rand7() 使用Rand7()来实现Rand10()

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:

[Swift]LeetCode470. 用 Rand7() 实现 Rand10() | Implement Rand10() Using Rand7()

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:

【LeetCode】Implement strStr()

Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. 标准KMP算法.可参考下文. http://blog.csdn.net/yaochunnian/article/details/7059486 核心思想在于求出模式串前缀与后缀中重复部分,将重复信息保存在n

LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)

翻译 用队列来实现栈的例如以下操作. push(x) -- 将元素x加入进栈 pop() -- 从栈顶移除元素 top() -- 返回栈顶元素 empty() -- 返回栈是否为空 注意: 你必须使用一个仅仅有标准操作的队列. 也就是说,仅仅有push/pop/size/empty等操作是有效的. 队列可能不被原生支持.这取决于你所用的语言. 仅仅要你仅仅是用queue的标准操作,你能够用list或者deque(double-ended queue)来模拟队列. 你能够如果全部的操作都是有效的(

LeetCode 232 Implement Queue using Stacks(用栈来实现队列)(*)

翻译 用栈来实现队列的下列操作. push(x) -- 将元素x写入到队列的尾部 pop() -- 从队列首部移除元素 peek() -- 返回队列首部元素 empty() -- 返回队列是否为空 注意: 你必须使用一个只有标准操作的栈. 也就是说,只有push/pop/size/empty等操作是有效的. 栈可能不被原生支持,这取决于你所用的语言. 只要你只是用stack的标准操作,你可以用list或者deque(double-ended queue)来模拟栈. 你可以假设所有的操作都是有效的