题目描述
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().
Example1:
Input: 1 Output: [7]
Example2:
Input: 2 Output: [8,4]
Example3:
Input: 3 Output: [8,1,10]
解题思路
- ```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() { /while(true) { int num = (rand7() - 1) * 7 + rand7(); if(num <= 10){ return num; } }/ while(true){ int num = (rand7() - 1) * 7 + rand7(); if(num <= 40){ return num = num % 10 + 1; } } } }; ```