'how to create random number in a range (2,10) with a mean of 4

I need to create numbers in a range with a specific mean. I tried using the RandBetween function in excel, however, I was not able to take into account the mean. Is there a way to get random numbers from a distribution that fit the criteria? Thank you



Solution 1:[1]

Assume there is a function rand() the returns an equal distribution random number between 0 and 1.

function myrand(minValue, maxValue, meanValue)
{
    tc = (maxValue - meanValue)/(maxValue - minValue);
    t = rand();
    if(t <= tc) 
    {
        return (1-t/tc)*minValue + t/tc*meanValue;
    } else {
        return (1-(t-tc)/(1-tc))*meanValue + (t-tc)/(1-tc)*maxValue;
    }
}

and call myrand(2.0,10.0,4.0)

Exmplanation:

create a bi-modal distribution with area under the curve equal to the mean value.

fig1

The x-axis goes between 0 and 1, and is the result of the rand() function, and the y-axis is the function return.

On average the mean value equals the area under the curve, something that is proved in calculus classes.

Solution 2:[2]

Given a probability mass function or a probability density function there are several algorithms able to extract pseudo-random numbers from it. In this Wikipedia page: https://en.wikipedia.org/wiki/Pseudo-random_number_sampling there are several examples and links.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 alessio lapolla