Math 使用给定的随机生成器函数生成从1到7的随机数

Math 使用给定的随机生成器函数生成从1到7的随机数,math,random,probability,Math,Random,Probability,创建一个函数,该函数以相等的概率生成数字1到7,使用一个函数以相等的概率随机生成数字1到5 我修改了前面的一个答案。对吗 #include <bits/stdc++.h> #define ran rand()%5 using namespace std; int main(){ int a[2][5] = {{1, 2, 3, 4, 5}, {6, 7, 0, 0, 0}}; int ct[8] = {0}; for(int i = 0; i<50000

创建一个函数,该函数以相等的概率生成数字1到7,使用一个函数以相等的概率随机生成数字1到5

我修改了前面的一个答案。对吗

#include <bits/stdc++.h>
#define ran rand()%5
using namespace std;
int main(){
    int a[2][5] = {{1, 2, 3, 4, 5}, {6, 7, 0, 0, 0}};
    int ct[8] = {0};
    for(int i = 0; i<50000000; i++){
        int j = ran;
        while(j>1){
            j = ran;
        }
        int k = ran;
        if(a[j][k]>0)
            ct[a[j][k]]++;
    }

    for(int i = 1; i<=7; i++){
        cout<<ct[i]<<endl;
    }
    return 0;
}

请告诉我们有什么不对的地方。

< P>这是一个现代C++解决方案:

#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <ostream>
#include <random>

template <unsigned int Min, unsigned int Max>
class imbued_urng
{
public:
    using result_type = unsigned int;

    static constexpr result_type min() { return Min; }
    static constexpr result_type max() { return Max; }

    imbued_urng(const std::function<result_type()>& source) : random{ source } {}
    imbued_urng(const imbued_urng& rhs) = delete;
    imbued_urng& operator=(const imbued_urng& rhs) = delete;

    result_type operator()() { return random(); }

private:
    const std::function<result_type()>& random;
};

int main()
{
    // Create a random number engine
    std::mt19937::result_type seed = std::random_device{}();
    std::cout << "seed = " << seed << std::endl;
    auto engine = std::mt19937{ seed };

    // Create the rand5 distribution
    auto const dist5{ std::uniform_int_distribution<> {1, 5} };
    auto const rand5{ std::function<unsigned int()>{ [&engine, &dist5] { return dist5(engine); } } };

    auto const n = 32;
    std::generate_n(std::ostream_iterator<unsigned int>(std::cout, " "), n, rand5);
    std::cout << std::endl;

    // Create a uniform random number generator based on rand5
    imbued_urng<1, 5> urng { rand5 };

    // Create the rand7 distribution
    auto const dist7{ std::uniform_int_distribution<> {1, 7} };
    auto const rand7{ std::function<unsigned int()>{ [&urng, &dist7] { return dist7(urng); } } };

    std::generate_n(std::ostream_iterator<unsigned int>(std::cout, " "), n, rand7);
    std::cout << std::endl;

    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
模板
阶级浸淫
{
公众:
使用结果类型=无符号整数;
静态constexpr结果_type min(){return min;}
静态constexpr结果_type max(){return max;}
嵌入的_urng(const std::function&source):随机{source}{
浸没的(const浸没的)&rhs)=删除;
嵌入的\u urng和运算符=(常量嵌入的\u urng和rhs)=删除;
结果_类型运算符()({return random();}
私人:
常数std::函数&随机;
};
int main()
{
//创建一个随机数引擎
std::mt19937::result_type seed=std::random_device{}();

std::cout不建议使用宏“模拟”函数(
ran
),但如果必须这样做,请学会正确保护扩展:
#定义ran(rand()%5)
否则,如果在表达式中使用宏,您可能会得到意外的结果。在这种情况下,使用宏的方式可能无关紧要,但对于其他以不同方式使用的宏,它会起作用。您将1-5映射到1-7的方式并不理想:我没有研究它以确定分布是否均匀,但主要问题是那是你在“问”对于5000万个数字,并且接近3500万。@TripeHound的问题是以相同的概率生成所有数字1到7。其余1500万个是0,我忽略了。@TripeHound:更糟糕的是,从1-5随机数生成器中抽取的数字将远远超过5000万:大约1.75亿(主循环的每次迭代涉及平均2.5次绘制以获得j,然后1次绘制以获得k),因此每个输出值平均5次绘制。虽然我相信OP的解决方案是正确的,但可以更有效地进行。理论极限为log(7)/log(5)每生成一次偏差~=1.209次绘制,不需要太多努力就可以接近这一点。@TripeHound:对,很公平。代码应该真正进行重构,这样就有了一个核心生成器函数,保证每次调用1到7次结果。这实际上是一个面试问题。我不认为这不是问题他希望得到答案。这可能是正确的,但我们希望得到更简单的答案。如果您能在我的实现中发现错误,我将不胜感激。@SurajTripathi您的解决方案肯定不是最好的,主要是因为所有的循环。此外,您还没有创建一个生成1到7之间的数字的函数(我怀疑这是他们想要的)…而
for
循环的“内部”实际上并不能做到这一点,因为有时它根本不会生成(有效)数字。至少,您可以在“有效性测试”中包装
for
的内部循环并将其粘贴到函数中——理想情况下,您会看到一种更有效的方法,使用多个1-5拾取来生成范围1-7
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <ostream>
#include <random>

template <unsigned int Min, unsigned int Max>
class imbued_urng
{
public:
    using result_type = unsigned int;

    static constexpr result_type min() { return Min; }
    static constexpr result_type max() { return Max; }

    imbued_urng(const std::function<result_type()>& source) : random{ source } {}
    imbued_urng(const imbued_urng& rhs) = delete;
    imbued_urng& operator=(const imbued_urng& rhs) = delete;

    result_type operator()() { return random(); }

private:
    const std::function<result_type()>& random;
};

int main()
{
    // Create a random number engine
    std::mt19937::result_type seed = std::random_device{}();
    std::cout << "seed = " << seed << std::endl;
    auto engine = std::mt19937{ seed };

    // Create the rand5 distribution
    auto const dist5{ std::uniform_int_distribution<> {1, 5} };
    auto const rand5{ std::function<unsigned int()>{ [&engine, &dist5] { return dist5(engine); } } };

    auto const n = 32;
    std::generate_n(std::ostream_iterator<unsigned int>(std::cout, " "), n, rand5);
    std::cout << std::endl;

    // Create a uniform random number generator based on rand5
    imbued_urng<1, 5> urng { rand5 };

    // Create the rand7 distribution
    auto const dist7{ std::uniform_int_distribution<> {1, 7} };
    auto const rand7{ std::function<unsigned int()>{ [&urng, &dist7] { return dist7(urng); } } };

    std::generate_n(std::ostream_iterator<unsigned int>(std::cout, " "), n, rand7);
    std::cout << std::endl;

    return 0;
}