Home > Software design >  how to create random number in a range (2,10) with a mean of 4
how to create random number in a range (2,10) with a mean of 4

Time:04-20

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

CodePudding user response:

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.

CodePudding user response:

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.

CodePudding user response:

If you are talking about a programming language you can use lists. *Using java here

First make an Array list

ArrayList testList= new ArrayList();

Then add your desired data into it manually (Since you dont have a lot of values)

testList.add("5"); *Like this add your data in.

Then to generate the value

Collections.shuffle(testList);

Then you can print the first or any value since its shuffled

System.out.println(testList.get(1))

Hope this helps

  •  Tags:  
  • math
  • Related