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

Posted Grandyang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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:

Input: 2
Output: [8,4]

Example 3:

Input: 3
Output: [8,1,10]

 

Note:

  1. rand7 is predefined.
  2. Each testcase has one argument: n, the number of times that rand10 is called.

 

Follow up:

  1. What is the expected value for the number of calls to rand7() function?
  2. Could you minimize the number of calls to rand7()?

 

s

 

以上是关于[LeetCode] Implement Rand10() Using Rand7() 使用Rand7()来实现Rand10()的主要内容,如果未能解决你的问题,请参考以下文章

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

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

leetcode470——用 Rand7() 实现 Rand10()

《LeetCode之每日一题》:139.用 Rand7() 实现 Rand10()

LC 470. Implement Rand10() Using Rand7()

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