首页 > 试题广场 >

用rand5()实现rand7()

[编程题]用rand5()实现rand7()
  • 热度指数:977 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
给定一个函数 rand5()可生成 [1,5] 范围内的均匀随机整数,请你试写一个方法 rand7 生成 [1,7] 范围内的均匀随机整数。
1.你只能调用 rand5() 且不能调用其他随机函数的方法。比如请不要使用系统的random() 方法,不要用题目里面类似于rand5的方法。
2.每个测试用例将有一个内部参数 n,表示你实现的函数 rand7() 在测试时将被调用的次数,但是这个参数不会被你看见,你可以在自测的时候使用。
3.该题判断你的代码是否正确的依据是:
3.1 你的代码不会陷入死循环,导致超时或者其他情况
3.2 你生成的数据是否都在[1,7]内
3.3 你生成的数据的期望是否接近4
3.4 你生成的数据在[1,7]是不是接近均匀分布

数据范围:

示例1

输入

1

输出

[2]
示例2

输入

2

输出

[1,5]
示例3

输入

3

输出

[1,3,7]
别打我
import java.util.*; 
 public class Solution { 
    public int rand5() { 
        return 1+(int)(Math.random()*5); 
    }

    public int rand7 () { 
        // write code here
        return 1+(int)(Math.random()*7); 
    } 
 }


发表于 2025-01-08 18:21:49 回复(0)
#include <iostream>
#include <map>
#include <random>
#include <unordered_map>

using std::cout;
using std::endl;
using std::map;
using std::unordered_map;
using std::vector;

class Solution {
    public:
        // rand5,离散均匀分布
        int rand5() {
            std::random_device rd;  // 用于生成随机数种子
            std::mt19937 gen(rd()); // 生成随机数引擎
            std::uniform_int_distribution<> distribution(1, 5); // 定义范围为 1~5 的均匀分布
            return distribution(gen);    
        }

        // 核心算法是,使用 1~5 的离散均匀分布构成 1~25 的离散均匀分布
        // 然后只保留 1~21,此时每个数值等概率出现 ,均为 1/25。
        // 将 1~21 映射到 0~6,使用取余运算,0~6 等待率出现,为 3/25。
        // 最后生成的随机数+1,0~6 对应 1~7。
        int rand7() {
            int target = -1;
            while (!(1 <= target && target <= 21)) {
                target = 5 * (rand5() - 1) + rand5();
            }
            return target % 7 + 1;
        }

        // 测试
        // 统计 n 轮随机数生成,数据的期望、分布情况。// rand5,离散均匀分布
        void test_rand7(int test_round) {
            int sum_rand = 0;
            int temp = -1;
            map<int, int> rand_count;
            vector<int> result(test_round, -1);
            for (int i = 0; i < test_round; i++) {
                temp = rand7();
                result[i] = temp;
                sum_rand += temp;
                if (rand_count.find(temp) == rand_count.end()) {
                    rand_count[temp] = 1;
                } else {
                    rand_count[temp]++;
                }
            }
            cout << "test " << test_round << " rounds." << endl;
            cout << "rand average is: " << double(sum_rand)/test_round << endl;
            cout << "rand frequency:" << endl;
            for (auto iter = rand_count.begin(); iter != rand_count.end(); iter++) {
                cout << "rand: " << iter->first 
                        << ", count: " << iter->second
                        << ", freq: " << double(iter->second)/test_round << endl;
            }
        }
    };

int main() {
    Solution instance;
    instance.test_rand7(10000);

    return 0;
}

发表于 2023-12-05 14:40:50 回复(0)
import java.lang.Math;

public class Solution {
    public int rand5() {
        return 1 + (int)(Math.random() * 5);
    }

    public int getRandomInt(int i, int j) {
        int a = i + (int)(Math.random() * (j - i));
        return a;
    }

    public int rand7 () {
        return getRandomInt(1, 8);
    }
}

发表于 2022-06-20 21:39:12 回复(0)
    public int rand7 () {
        // write code here
        int v = 5 * rand5() + rand5() - 5;
        if(v > 21) {
            return rand7();
        }
        return v % 7 + 1;
    }

发表于 2022-03-02 18:08:13 回复(0)

问题信息

上传者:牛客301499号
难度:
4条回答 2230浏览

热门推荐

通过挑战的用户

查看代码
用rand5()实现rand7()