1. class Solution {
    2. public:
    3. int rand10() {
    4. int a, b, idx;
    5. while (true) {
    6. a = rand7();
    7. b = rand7();
    8. idx = b + (a - 1) * 7;
    9. if (idx <= 40)
    10. return 1 + (idx - 1) % 10;
    11. a = idx - 40;
    12. b = rand7();
    13. // get uniform dist from 1 - 63
    14. idx = b + (a - 1) * 7;
    15. if (idx <= 60)
    16. return 1 + (idx - 1) % 10;
    17. a = idx - 60;
    18. b = rand7();
    19. // get uniform dist from 1 - 21
    20. idx = b + (a - 1) * 7;
    21. if (idx <= 20)
    22. return 1 + (idx - 1) % 10;
    23. }
    24. }
    25. };