-1

I want to generate a random age between 8 and 100. Such as Random.Shared.Next(8,100) However I want the likelihood of ages to be more likely in say around 30 being the most likely and it becoming progressively less likely going towards the other 2 ends.

So all I can think of to achieve it would be generating multiple sets and averaging them.

F Dev
  • 82
  • 8
  • There are several alorithms to convert a uniform probability displibution to another one, e.g. a gaussian distribution. See e.g. https://stackoverflow.com/questions/218060/random-gaussian-variables, or https://stackoverflow.com/questions/1303368/how-to-generate-normally-distributed-random-from-an-integer-range – Klaus Gütter Jan 23 '22 at 04:34

1 Answers1

0

This is probably a naïve approach but you could split the random generation into multiple steps.

For example, say you want 70% of the ages to be between 20 and 70, you could introduce an additional random check:

if (random.NextDouble() >= 0.3)
{
    return random.Next(20, 70); // generate ages 20, 21, ..., 68, 69
}

Now, we're left with the other 30%. I'd assume that there will be more young people than old people, so I'll attribute 20% to young people, and 10% to old people. We can update the code above to handle this:

double percentage = random.NextDouble();

if (percentage >= 0.3) // 1.0 - 0.3 = 70%
{
    return random.Next(20, 70);
}
else if (percentage >= 0.1) // 0.3 - 0.1 = 20%
{
    return random.Next(0, 20);
}
else // remaining 10%
{
    return random.Next(70, 100);
}

Try it online - in the sample I bucket ages into the nearest 5 and then provide the count for the distribution.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86