Jeroen
Jeroen

Reputation: 16855

Make Random Numbers Tend / Average to a Specific Value

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 what numbers got output. This question is not really C++ related though.

#include <iostream>
#include <random>
#include <time.h>
int main(int argCount, char** argVector) {
    std::cout << "Generating Randoms" << std::endl;

    float avarage = 0.F;
    srand(rand() + (int) time(NULL));
    float ceiling = 0;
    float bottom = 1;
    for(unsigned int i = 0; i < 1000000; i++) {
        float random = (float) (rand() % 101) / 100;
        if(random > ceiling)
            ceiling = random;
        else if(random < bottom)
            bottom = random;
        avarage += random;
    }
    std::cout << "Avarage: " << avarage/1000000 << std::endl;
    std::cout << "Ceiling: " << ceiling << std::endl;
    std::cout << "Bottom: " << bottom << std::endl;
    return 0;
}

This outputs:

Generating Randoms
Avarage: 0.499287
Ceiling: 1
Bottom: 0

I would like the ceiling and bottom to be still 0 and 1, but be able to change the average. The algorithm should preferably be efficient too.

Once again, I'm now posting C++ code, but any language will do.

Upvotes: 0

Views: 2017

Answers (3)

pjs
pjs

Reputation: 19855

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.

Upvotes: 4

seth
seth

Reputation: 1788

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.

Upvotes: 2

NolanPower
NolanPower

Reputation: 409

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

Upvotes: 1

Related Questions