📜  随机标头|套装2(分配)

📅  最后修改于: 2021-05-30 14:07:04             🧑  作者: Mango

一组1(发电机)

发行版

一,制服: 制服

  1. uniform_int_distribution:产生随机整数值i,它们均匀分布在闭合区间[a,b]上,由以下概率质量函数描述:
    功能
    • 运算符():生成根据概率函数分布的随机数。
    • min:它返回由运算符()返回的值范围的最大下限,该范围是uniform_int_distribution的分布参数“ a”。
    • max:它返回由运算符()返回的值范围的最小上限,该上限是uniform_int_distribution的分布参数“ b”。
    • reset:重置发行版,以便在后续使用中,结果不取决于该发行版已产生的值。
    // C++ program to illustrate
    // the use of operator()
    // in uniform_int_distribution
    #include 
    #include 
    using namespace std;
      
    // Driver program
    int main()
    {
          
        // Constructing a trivial random generator engine 
        unsigned s = 2;
          
        // The random number generator
        default_random_engine generator (s);
          
        uniform_int_distribution distribution(1,10);
        cout << "Some random numbers between 1 and 10";
        for (int i = 0; i < 10; ++i)
            cout << distribution(generator) ;
          
        cout << endl;
          
        return 0;
    }
    

    输出:

    Some random numbers between 1 and 10: 1 3 6 10 1 5 1 4 4 9 
    
    // C++ program to illustrate
    // the use of reset
    // in uniform_int_distribution
    #include 
    #include 
    using namespace std;
      
    //Driver program
    int main()
    {
              
        //the random number generator
        default_random_engine generator;
          
        // Initialising the uniform distribution
        uniform_int_distribution distribution(1, 1000);
          
        // First random number is generated
        cout << distribution(generator) << endl;
          
        //Resets the distribution
        distribution.reset();
          
        // Second random number is 
        //generated independent of previous number
        cout << distribution(generator) << endl;
          
        return 0;
    }
    

    输出:

    1
    132
    
  2. uniform_real_distribution:它是产生浮点值的随机数分布,由以下概率密度函数描述:
    Uniform_real
    • 运算符():它会返回一个新的随机数,该随机数紧跟发行版的参数。
    • min:返回运算符()返回的值范围的最大下限,该范围是uniform_real_distribution的分布参数“ a”。
    • max:返回由运算符()返回的值范围的最小上限,该范围是uniform_real_distribution的分布参数“ b”。
    • reset:重置发行版,以便在后续使用中,结果不取决于该发行版已产生的值。
    // C++ program to illustrate
    // the use of operator()
    // in uniform_int_distribution
    #include 
    #include 
    using namespace std;
      
    // Driver program
    int main()
    {
          
        // Constructing a trivial random generator engine 
        unsigned s = 2;
          
        // The random number generator
        default_random_engine generator (s);
          
        uniform_int_distribution distribution(1,10);
        cout << "Random numbers between 1 and 10";
        for (int i = 0; i< 10; ++i)
            cout << distribution(generator) ;
          
        cout << endl;
          
        return 0;
    }
    

    输出:

    some random numbers between 0.0 and 10.0: 
    0.150031
    9.77072
    3.36669
    7.06447
    5.11455
    8.43061
    1.93792
    7.78965
    8.31532
    5.14354
    
    // C++ program to illustrate
    // the use of reset
    // in uniform_real_distribution
    #include 
    #include 
    using namespace std;
      
    // Driver program
    int main()
    {
        default_random_engine generator;
        uniform_real_distribution distribution(0.0,100.0);
          
        // It prints two independent values:
        // First random number is generated
        cout << distribution(generator) << endl;
          
        //Resets the distribution
        distribution.reset();
          
        // Second random number is 
        //generated independent of previous number
        cout << distribution(generator) << endl;
          
        return 0;
    }
    

    输出:

    13.1538
    45.865
    

二。与bernoulli试验有关:
与伯努利试验有关

  1. bernoulli_distribution:它是根据伯努利分布产生布尔值的随机数分布,由以下概率质量函数:
    功能
    • 运算符():返回一个新的随机数。
    • min:返回运算符()返回的值范围的最大下限,对于bernoulli_distribution为false。
    • max:返回由运算符()返回的值范围的最小上限,对于bernoulli_distribution为true。
    // C++ program to illustrate
    // the bernoulli_distribution
    #include 
    #include 
    using namespace std;
      
    //Driver program
    int main()
    {
        const int temp=500;
          
        //The random number generator
        default_random_engine generator;
          
        //Initialising the bernoulli distribution
        bernoulli_distribution distribution(0.7);
          
        // count number of trues
        int count=0; 
          
        for (int i = 0; i < temp; ++i) 
        {
              
            // checking for true condition 
            if (distribution(generator)) 
            count++;
        }
          
        cout << "bernoulli_distribution (0.7) x 500:" << endl;
        cout << "true: " << count << endl;
        cout << "false: " << temp-count << endl;
          
        return 0;
    }
    

    输出:

    bernoulli_distribution (0.7) x 500:
    true:  360
    false: 140
    
    // C++ program to
    // illustrate the use of reset
    #include 
    #include 
    using namespace std;
      
    //Driver program
    int main()
    {
        // Random number generator
        default_random_engine generator;
          
        // Initialising the bernoulli distribution
        bernoulli_distribution distribution;
          
        // print two independent values:
        cout << distribution(generator) << endl;
          
        // use of reset
        // Generates second output without
        // the effect of first output 
        distribution.reset();
        cout << distribution(generator) << endl;
      
    return 0;
    }
    

    输出:

    1
    1
    
  2. binomial_distribution:它是根据二项式离散分布生成整数的随机数分布,它由以下概率质量函数:
    二项分布
    • 运算符():生成一个新的随机数。
    • max:返回由运算符()给出的范围的最小上限,对于binomial_distribution,它是分布参数t。
    • min:返回成员运算符()给出的范围的最大下限,对于binomial_distribution,该下限始终为零。
    • reset:重置分布,以便对象的后续使用不依赖于它已经产生的值。
    // C++ program to illustrate
    // the use of binomial_distribution
    #include 
    #include 
    #include 
    using namespace std;
      
    int main()
    {
          
        // construct a trivial random 
        //generator engine from a time-based seed:
        unsigned seed = chrono::system_clock::now().time_since_epoch().count();
        default_random_engine generator (seed);
          
        // Initialising binomial distribution
        binomial_distribution distribution (15, 0.4);
          
        cout << "some binomial results (t=15, p=0.4): ";
        for (int i = 0; i < 15; ++i)
        {
                  
            // Use of operator()
            cout << distribution(generator) << " ";
        }
        cout << endl;
          
        return 0;
    }
    

    输出:

    some binomial results (t=15, p=0.4): 7 6 7 8 4 6 7 6 9 3 5 6 4 6 7 
    
    // C++ program to illustrate
    // the use of binomial_distribution
    #include 
    #include 
    #include 
    using namespace std;
      
    int main()
    {
          
        // construct a trivial random 
        //generator engine from a time-based seed:
        unsigned seed = chrono::system_clock::now().time_since_epoch().count();
        default_random_engine generator (seed);
          
        // Initialising binomial distribution
        binomial_distribution distribution (15, 0.4);
          
        cout << "some binomial results (t=15, p=0.4): ";
        for (int i = 0; i < 15; ++i)
        {
                  
            // Use of operator()
            cout << distribution(generator) << " ";
        }
        cout << endl;
          
        return 0;
    }
    

    输出:

    57
    52
    
  3. geometric_distribution:它是一个随机数分布,它根据几何离散分布生成整数,由以下概率质量函数:
    geometric_distribution
    • 运算符():它会返回一个新的随机数,该随机数紧跟发行版的参数。
    • max:返回运算符()给定范围的最小上限。
    • min:返回运算符()给出的最小值。
    • reset:重置分布,以便对象的后续使用不依赖于它已经产生的值。
    // C++ program to illustrate
    //the use of geometric_distribution 
    #include 
    #include 
    #include 
    #include 
    using namespace std;
      
    int main()
    {
        // construct a trivial random
        // generator engine from a time-based seed:
        int seed = chrono::system_clock::now().time_since_epoch().count();
        default_random_engine generator (seed);
          
        // Initialises the geometric distribution
        geometric_distribution distribution (1.0 / 5);
          
        cout << "Plus sign is 5 spaces away from the next :" << endl;
        for (int i = 0; i < 10 ; ++i) 
        {
            int number = distribution(generator);
            cout << string (number,' ') << "+";
        }
      
    return 0;
    }
    

    输出:

    each plus sign is 5 spaces away from the next :
                ++ + +   +  ++     +        ++
    
    // C++ program to illustrate 
    // the use of reset
    #include 
    #include 
    using namespace std;
      
    // Driver program
    int main()
    {
          
        // Random number generator 
        default_random_engine generator;
          
        // Initialising the geometric distribution
        geometric_distribution distribution(0.3);
          
        // Prints two independent values:
        // Generates the first value
        cout << distribution(generator) << endl;
          
        // Use of reset
        distribution.reset();
          
        // Generates second value
        cout << distribution(generator) << endl;
          
        return 0;
    }
    

    输出:

    0
    1
    
  4. negative_binomial_distribution:这是一个随机数分布,根据负二项式离散分布(也称为Pascal分布)产生整数,由以下概率质量函数:
    消极的
    • 运算符():它会返回一个新的随机数,该随机数紧跟发行版的参数。
    • max:返回运算符()给定范围的最小上限。
    • min:返回由运算符()给出的最小值,对于negative_binomial_distribution始终为零。
    • reset:重置分布,以便对象的后续使用不依赖于它已经产生的值。
    // C++ program to illustrate
    // the use of operator() in
    // negative_binomial_distribution 
    #include 
    #include 
    #include 
    using namespace std;
      
    // Driver program
    int main()
    {
        // construct a trivial random 
        // generator engine from a time-based seed:
        unsigned seed = chrono::system_clock::now().time_since_epoch().count();
        default_random_engine generator (seed);
          
        // Initialising negative binomial distribution
        negative_binomial_distribution distribution (6,0.7);
          
        cout << "Negative binomial results (t=6, p=0.7): ";
        for (int i = 0; i < 15; ++i)
            {
                // Use of operator
                cout << distribution(generator) << " ";
            }
          
        cout << endl;
          
        return 0;
    }
    

    输出:

    Negative binomial results (t=6, p=0.7): 2 6 3 1 4 1 4 1 2 0 7 3 4 4 4 
    
    // C++ program to illustrate
    // the use of reset in
    // negative_binomial_distribution::
    #include 
    #include 
    using namespace std;
      
    // Driver program
    int main()
    {
          
        // Random number generator 
        default_random_engine generator;
          
        // Initialising the negative binomial distribution
        negative_binomial_distribution distribution(20, 0.5);
          
        // print two independent values:
        // Generates the first value
        cout << distribution(generator) << endl;
          
        // Use of reset
        distribution.reset();
          
        // Generates the second value
        cout << distribution(generator) << endl;
          
        return 0;
    }
    

    输出:

    23
    30
    

三,明智的分配方式:
逐段地

  1. 离散分布这是一个随机数分布,根据离散分布生成整数值。
    • 运算符():它会返回一个新的随机数,该随机数紧跟分布的参数。
    • max:返回运算符()给定范围的最小上限。
    • min:返回运算符()给定范围的最大下限。
    • reset:重置分布,以便对象的后续使用不依赖于它已经产生的值。
    // C++ program to illustrate the
    // use of operator() in
    // discrete_distribution
    #include 
    #include 
    using namespace std;
      
    int main()
    {
          
        // number of experiments
        int n = 10000; 
          
        // maximum number of stars to distribute
        int m = 100; 
          
        // Random number generator
        default_random_engine generator;
          
        //Initialising discrete distribution
        discrete_distribution distribution { 2, 2, 1, 1, 2, 2, 1, 1, 2, 2 };
          
        int p[10] = {};
          
        // use of operator()
        for (int i = 0; i < n; i++) 
        {
            int number = distribution(generator);
            p[number]++;
        }
          
        cout << "a discrete_distribution:" << endl;
        for (int i = 0; i < 10; ++i)
        {
            cout << i << ": " << string(p[i]*m/n,'*') << endl;
        }
          
        return 0;
    }
    

    输出:

    a discrete_distribution:
    0: ************
    1: *************
    2: *****
    3: ******
    4: ************
    5: ************
    6: ******
    7: ******
    8: ************
    9: ************
    
    // C++ program to illustrate
    //the use of reset in
    //discrete_distribution
    #include 
    #include 
    using namespace std;
      
    // Driver program
    int main()
    {
          
        // Random number generator 
        default_random_engine generator;
          
        // Initialising the discrete distribution
        discrete_distribution distribution {20,20,30,40};
          
        // print two independent values:
        // Generates the first value
        cout << distribution(generator) << endl;
          
        // Use of reset
        distribution.reset();
          
        // Generates the secong value
        cout << distribution(generator) << endl;
          
        return 0;
    }
    

    输出:

    0
    2
    
  2. piecewise_constant_distribution:这是一个随机数分布,会产生浮点值,这些浮点值均匀分布在一系列连续子间隔的每个序列上,由以下概率密度函数:
    压电常数
    • 运算符():它会返回一个新的随机数,该随机数紧跟发行版的参数。
    • max:返回由运算符()给出的范围的最小上限。
    • min:返回运算符()给定范围的最大下限。
    • reset:重置分布,以便对象的后续使用不依赖于它已经产生的值。
    // C++ program to illustrate the
    // use of reset in
    // piecewise_constant_distribution
    #include 
    #include 
    using namespace std;
      
    // Driver program
    int main()
    {
          
        // Random number generator
        default_random_engine generator;
          
        // Initialisind piecewise_constant_distribution
        piecewise_constant_distribution distribution 
                    ( 4, 0.0, 10.0, [](double x){return x;} );
          
        // print two independent values:
        // Generates the first value
        // Use of operator()
        cout << distribution(generator) << endl;
          
        // Use of reset
        distribution.reset();
          
        // Generates second value
        cout << distribution(generator) << endl;
          
        return 0;
    }
    

    输出:

    3.4205
    6.6692
    
  3. piecewise_linear_distribution:这是一个随机数分布,会产生浮点值,这些浮点值分布在一系列连续的子间隔上。
    • 运算符():它会返回一个新的随机数,该随机数紧跟发行版的参数。
    • max:返回运算符()给定范围的最小上限。
    • min:返回运算符()给定范围的最大下限。
    • reset:重置分布,以便对象的后续使用不依赖于它已经产生的值。
    // C++ program to illustrate the
    // use of reset in
    // piecewise_linear_distribution
    #include 
    #include 
    using namespace std;
      
    // Driver program
    int main()
    {
          
        // Random number generator
        default_random_engine generator;
          
        // Initialising piecewise_linear_distribution
        piecewise_linear_distribution
            distribution ( 5, 0.0, 10.0, [](double x){return x+1.0;} );
          
        // print two independent values:
        // generates first value
        // use of operator()
        cout << distribution(generator) << endl;
          
        // Use of reset
        distribution.reset();
          
        // generates second value
        cout << distribution(generator) << endl;
          
        return 0;
    }
    

    输出:

    2.48143
    6.07656
    
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”