如何生成具有(单独)最小/最大限制的随机双精度?

时间:2016-08-04 03:22:52

标签: c# math random

这就是我想要实现的目标:

public double NextMin (double min)
{
    //Returns a double that is greater than or equal to "min". 
}

public double NextMax (double max)
{
    //Returns a double that is lesser than "max".
}

我尝试了范围扩展,但它返回了很多" Infinity"由于溢出:

// Range expanding for min
random.NextDouble() * (double.MaxValue - min) + min;

// Range expanding for max 
random.NextDouble() * (max - double.MinValue) + double.MinValue;

澄清:我需要尽可能多的返回范围,这意味着它们应该包括底片和正片。

4 个答案:

答案 0 :(得分:4)

我认为这适用于一般情况(假设min小于max)。

private Random rng = new Random();

private double GetRandomDouble(double min, double max)
{
    var half_min = min / 2.0;
    var half_max = max / 2.0;
    var average = half_min + half_max;
    var factor = max - average;

    return (2.0 * rng.NextDouble() - 1.0) * factor + average;
}

调用Console.WriteLine(GetRandomDouble(double.MinValue, double.MaxValue));然后在double.MinValuedouble.MaxValue之间正确生成值。

然后你就可以充实你想要的两种方法,如下:

public double NextMin(double min)
{
    return GetRandomDouble(min, double.MaxValue);
}

public double NextMax(double max)
{
    return GetRandomDouble(double.MinValue, max);
}

答案 1 :(得分:-1)

private Random rng = new Random();

private double GetRandomDouble(double min, double max)
{
    // Get the base value, scale first and then shift.
    return rng.NextDouble()*(max - min) + min;
}
编辑:我有点困惑,为什么这对我有用而不对别人有用。我刚刚运行这个代码是VS 2015.3而不是一次三次运行它是否检测到无限值:

class Program
{
    static void Main(string[] args)
    {
        const int maxIterations = 100000;
        var infinityDetected = false;

        for (int i = 0; i < maxIterations; i++)
        {
            var d = GetRandomDouble(double.MinValue, double.MaxValue);

            if (double.IsInfinity(d))
            {
                infinityDetected = true;
                Console.WriteLine("Infinity detected");
                break;
            }

            Console.WriteLine(d);
        }

        if (!infinityDetected)
        {
            for (int i = 0; i < maxIterations; i++)
            {
                var d = GetRandomDouble(-1.0, double.MaxValue);

                if (double.IsInfinity(d))
                {
                    infinityDetected = true;
                    Console.WriteLine("Infinity detected");
                    break;
                }

                Console.WriteLine(d);
            }
        }

        if (!infinityDetected)
        {
            for (int i = 0; i < maxIterations; i++)
            {
                var d = GetRandomDouble(double.MinValue, 1.0);

                if (double.IsInfinity(d))
                {
                    Console.WriteLine("Infinity detected");
                    break;
                }

                Console.WriteLine(d);
            }
        }

        Console.ReadLine();
    }

    private static Random rng = new Random();

    private static double GetRandomDouble(double min, double max)
    {
        // Get the base value, scale first and then shift.
        return rng.NextDouble() * (max - min) + min;
    }
}

答案 2 :(得分:-1)

这应该可以更好地发挥溢出效果。

append

第二个实际上与您发布的那个相同,我不明白为什么 一个人无法工作。

答案 3 :(得分:-2)

试试这个:

filename