Make Random Numbers Tend / Average to a Specific Value

前端 未结 3 632

How can I for example generate a list of random numbers between 0 and 1, but have them avarage at 0.8?

I have written this little script in C++ that\'ll tell you wh

相关标签:
3条回答
  • 2020-12-22 06:36

    NolanPower had a great idea using powers, but the mechanism he recommended for choosing the power is off. If the random numbers U are uniform(0,1) the law of the unconscious statistician says we can derive the expected value of any function g(U) as Integral[g(U) from: 0 to: 1]. If our function g(U) is a polynomial, i.e., U**c for some constant c, evaluating the integral yields the general solution 1 / (c + 1) as the expected value. Setting this equal to the desired mean m and solving, we get that c = (1 / m) - 1.

    To get an expected value of 0.8, c = (1 / 0.8) - 1 = 0.25, i.e., crank out U**0.25. To get an expected value of 0.2, c = (1 / 0.2) - 1 = 4, i.e., generate values using U**4.

    0 讨论(0)
  • 2020-12-22 06:36

    Here's an example that generates a standard normal distribution, i.e. mu = 0, sigma = 1.

    I used the Box-Muller transform.

    All plots have x axis = value and y axis = frequency.

    #include <iostream>
    #include <random>
    #include <time.h>
    #include <math.h>
    int main(int argCount, char** argVector) {
        const double pi = 3.14159265359;
        const double nums = 1000000;
        double u, v, x;
    
        srand(rand() + (int) time(NULL));
    
        for(unsigned int i = 0; i < nums; i++){
            u = rand() / (((double)RAND_MAX) + 1.0);
            v = rand() / (((double)RAND_MAX) + 1.0);
            x = sqrt(-2*log(u)) * cos(2*pi*v);
    
            if (std::isfinite(x)){
                std::cout << x <<" ";
            }
        }
    
        return 0;
    }
    

    standardnorm

    >>> np.std(nums)
    1.0004139708929858
    >>> np.average(nums)
    7.1785002756408726e-05
    

    You can shift/scale x as necessary to obtain a mu and sigma of your choosing.

    Here's an example that gives a uniform distribution with a given mu:

    #include <iostream>
    #include <random>
    #include <time.h>
    #include <math.h>
    int main(int argCount, char** argVector) {
        const double pi = 3.14159265359;
        const double nums = 1000000;
        double x,mu;
    
        srand(rand() + (int) time(NULL));
        mu = 3.0;
    
        for(unsigned int i = 0; i < nums; i++){
            x = rand() / (((double)RAND_MAX) + 1.0);
            x *= 2*mu;
    
            if (std::isfinite(x)){
                std::cout << x <<" ";
            }
        }
    
        return 0;
    }
    

    unif

    >>> np.average(nums)
    3.0003091558133184
    

    You can use the documented rand() % range + min to truncate.

    0 讨论(0)
  • 2020-12-22 06:47

    Raise your number to the .321928 power will make the average .8 and still range from 0-1.

    0 讨论(0)
提交回复
热议问题