Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random number generator which gravitates numbers to any given number in range?

Tags:

c#

random

I'm trying to come up with a random number generator which returns a float value between 0 and 1 weights the returned value in either one direction or the other.

Is there any reliable way to do this based on passing in two numbers, say Random(0.5) where '0.5' means the number between 0 and 1 returned will gravitate towards 0.5.

like image 525
meds Avatar asked Oct 28 '25 14:10

meds


1 Answers

What you are referring to is a random number projected onto a bell curve

for which i generally do the following

/// <summary>
/// generate a random number where the likelihood of a large number is greater than the likelihood of a small number
/// </summary>
/// <param name="rnd">the random number generator used to spawn the number</param>
/// <returns>the random number</returns>
public static double InverseBellCurve(Random rnd)
{
    return 1 - BellCurve(rnd);
}
/// <summary>
/// generate a random number where the likelihood of a small number is greater than the likelihood of a Large number
/// </summary>
/// <param name="rnd">the random number generator used to spawn the number</param>
/// <returns>the random number</returns>
public static double BellCurve(Random rnd)
{
    return  Math.Pow(2 * rnd.NextDouble() - 1, 2);
}
/// <summary>
/// generate a random number where the likelihood of a mid range number is greater than the likelihood of a Large or small number
/// </summary>
/// <param name="rnd">the random number generator used to spawn the number</param>
/// <returns>the random number</returns>
public static double HorizontalBellCurve(Random rnd)
{
    //This is not a real bell curve as using the cube of the value but approximates the result set
    return  (Math.Pow(2 * rnd.NextDouble() - 1, 3)/2)+.5;
}

Note you can tweak the formulas to change the shape of the bell to adjust the distribution of the results

for eample a simple Math.Sqrt(rnd.nextdouble()) will slant all numbers towards 1, and a simple Math.Power(rnd.nextdouble(),2) will slant the results towards 0

like image 161
MikeT Avatar answered Oct 31 '25 06:10

MikeT