随机数发生器将数字吸引到范围内的任何给定数字?

时间:2011-04-28 10:11:50

标签: c# random

我正在尝试提出一个随机数生成器,它返回0到1之间的浮点值,以一个方向或另一个方向对返回值进行加权。

有没有可靠的方法来传递两个数字,比如Random(0.5),其中'0.5'表示返回的0到1之间的数字将倾向于0.5。

2 个答案:

答案 0 :(得分:2)

这可能有帮助吗?

http://www.c-sharpcorner.com/UploadFile/trevormisfeldt/NormalDistribution08302005003434AM/NormalDistribution.aspx

它无法准确解决您的问题,但我想知道建模钟形曲线是否可能提供您正在寻找的趋势。

有趣的问题,我可以问一下你要解决的问题吗?

第二次编辑:我刚刚注意到另一个可能有用的S / O问题:

Random number within a range based on a normal distribution

答案 1 :(得分:2)

您所指的是投射到钟形曲线上的随机数

我通常会做以下事情

/// <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;
}

请注意,您可以调整公式以更改钟形的形状以调整结果的分布

例如,一个简单的Math.Sqrt(rnd.nextdouble())会将所有数字倾斜为1, 一个简单的Math.Power(rnd.nextdouble(),2)将结果倾斜为0

相关问题