Make Random Numbers Tend / Average to a Specific Value

那年仲夏 提交于 2019-11-29 17:45:49

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.

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

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;
}

>>> 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;
}

>>> np.average(nums)
3.0003091558133184

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!