Random Number Between 2 Double Numbers

后端 未结 12 2074
粉色の甜心
粉色の甜心 2020-11-27 10:25

Is it possible to generate a random number between 2 doubles?

Example:

public double GetRandomeNumber(double minimum, double maximum)
{
    return Ra         


        
相关标签:
12条回答
  • 2020-11-27 11:03

    What if one of the values is negative? Wouldn't a better idea be:

    double NextDouble(double min, double max)
    {
           if (min >= max)
                throw new ArgumentOutOfRangeException();    
           return random.NextDouble() * (Math.Abs(max-min)) + min;
    }
    
    0 讨论(0)
  • 2020-11-27 11:05

    The simplest approach would simply generate a random number between 0 and the difference of the two numbers. Then add the smaller of the two numbers to the result.

    0 讨论(0)
  • 2020-11-27 11:06

    Watch out: if you're generating the random inside a loop like for example for(int i = 0; i < 10; i++), do not put the new Random() declaration inside the loop.

    From MSDN:

    The random number generation starts from a seed value. If the same seed is used repeatedly, the same series of numbers is generated. One way to produce different sequences is to make the seed value time-dependent, thereby producing a different series with each new instance of Random. By default, the parameterless constructor of the Random class uses the system clock to generate its seed value...

    So based on this fact, do something as:

    var random = new Random();
    
    for(int d = 0; d < 7; d++)
    {
        // Actual BOE
        boes.Add(new LogBOEViewModel()
        {
            LogDate = criteriaDate,
            BOEActual = GetRandomDouble(random, 100, 1000),
            BOEForecast = GetRandomDouble(random, 100, 1000)
        });
    }
    
    double GetRandomDouble(Random random, double min, double max)
    {
         return min + (random.NextDouble() * (max - min));
    }
    

    Doing this way you have the guarantee you'll get different double values.

    0 讨论(0)
  • 2020-11-27 11:06

    Use a static Random or the numbers tend to repeat in tight/fast loops due to the system clock seeding them.

    public static class RandomNumbers
    {
        private static Random random = new Random();
        //=-------------------------------------------------------------------
        // double between min and the max number
        public static double RandomDouble(int min, int max) 
        {
            return (random.NextDouble() * (max - min)) + min;
        }
        //=----------------------------------
        // double between 0 and the max number
        public static double RandomDouble(int max) 
        {
            return (random.NextDouble() * max);
        }
        //=-------------------------------------------------------------------
        // int between the min and the max number
        public static int RandomInt(int min, int max) 
        {   
            return random.Next(min, max + 1);
        }
        //=----------------------------------
        // int between 0 and the max number
        public static int RandomInt(int max) 
        {
            return random.Next(max + 1);
        }
        //=-------------------------------------------------------------------
     }
    

    See also : https://docs.microsoft.com/en-us/dotnet/api/system.random?view=netframework-4.8

    0 讨论(0)
  • 2020-11-27 11:11

    You could do this:

    public class RandomNumbers : Random
    {
        public RandomNumbers(int seed) : base(seed) { }
    
        public double NextDouble(double minimum, double maximum)
        {
            return base.NextDouble() * (maximum - minimum) + minimum;
        }
    }
    
    0 讨论(0)
  • 2020-11-27 11:15

    Yes.

    Random.NextDouble returns a double between 0 and 1. You then multiply that by the range you need to go into (difference between maximum and minimum) and then add that to the base (minimum).

    public double GetRandomNumber(double minimum, double maximum)
    { 
        Random random = new Random();
        return random.NextDouble() * (maximum - minimum) + minimum;
    }
    

    Real code should have random be a static member. This will save the cost of creating the random number generator, and will enable you to call GetRandomNumber very frequently. Since we are initializing a new RNG with every call, if you call quick enough that the system time doesn't change between calls the RNG will get seeded with the exact same timestamp, and generate the same stream of random numbers.

    0 讨论(0)
提交回复
热议问题