mercredi 3 août 2016

How to generate a random double with (seperately) minimum / maximum limits?

This is what I want to achieve:

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".
}

I tried range expanding but it returns lots of "Infinity" due to overflow:

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

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


Clarification: I need the return ranges to be as great as possible, which means they should include both negatives and positives.




Aucun commentaire:

Enregistrer un commentaire