Can you solve this real interview question? Implement Rand10() Using Rand7() - Given the API rand7() that generates a uniform random integer in the range [1, 7], write a function rand10() that generates a uniform random integer in the range [1, 10]. You
已提供一个Rand7()的API可以随机生成1到7的数字,使用Rand7实现Rand10,Rand10可以随机生成1到10的数字。 2. 思路 简单说:(1)通过(Rand N - 1) % 10 + 1的方法,可以求出Rand10,当N是10的倍数的时候。(2)用( Rand7 - 1 ) * 7 + Rand7可以随机生成1-49,记作Rand49。(3)如果可以通过Rand49计算...
classSolution {public:intrand10() {intnum = (rand7() -1) *7+rand7();return(num <=40) ? (num %10+1) : rand10(); } }; 我们还可以对上面的解法进行一下优化,因为说实话在 [1, 49] 的范围内随机到 [41, 49] 内的数字概率还是挺高的,我们可以做进一步的处理,就是当循环到这九个数字...
再用randX()生成randM(),如这题中的 49 —> 40 —> 10。 举个例子,用rand3()生成rand11(),可以先生成rand27(),然后变成以 22 为界限,因为 22 是 11 的倍数。生成rand27()的方式:3 * 3 * (rand3() - 1) + 3 * (rand3() - 1) + (rand3() - 1),最后生成了rand11();用rand7()...
/* * @lc app=leetcode id=470 lang=cpp * * [470] Implement Rand10() Using Rand7() */ // 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() { int index = INT_MAX; while...
rand7is predefined. Each testcase has one argument:n, the number of times thatrand10is called. Follow up: What is theexpected valuefor the number of calls torand7()function? Could you minimize the number of calls torand7()? 题解: ...
470. Implement Rand10() Using Rand7() 解题思路 方法:拒绝采样 已知Rand7()生成[1,7]的随机数的概率为1/7,如果取到7,我们拒绝再重新进行采样,即我们放弃取到7的所有结果,这种情况下,我们就得到了Rand6()。 同理,我们拒绝取到6,7的采样,则得到Rand5()。
已有方法 rand7 可生成 1 到 7 范围内的均匀随机整数,试写一个方法 rand10 生成 1 到 10 范围内的均匀随机整数。 不要使用系统的 Math.random() 方法。 示例: 示例1: 输入: 1 输出: [7] 示例2: 输入: 2 输出: [8,4] 示例3: 输入: 3 ...
— A Book for All Readers • Ainsworth Rand Spofford Read full book for free! ... Ibid., p. 103. This implement is identical with the "yoke" so often mentioned in the Old and New Testament as an emblem of bondage and labour; and figured, with the same significance; on Grecian ...
W3 = 0.5-rand(Nh, 10); b3 = zeros(10,1); % set up a sigmoid activation function for layers 2 and 3 sig2 = @(x) 1./(1+exp(-x)); dsig2 = @(x) exp(-x)./((1+exp(-x)).^2); if cf == 1; sig3 = @(x) 1./(1+exp(-x...