Home > OS >  Random minvalue cannot be greater than maxvalue
Random minvalue cannot be greater than maxvalue

Time:04-03

I am trying to make a console based application in which the program will subtract two random numbers from a range provided by the user but when i enter the values it give me a "Random min value cannot be greater than maxvalue" error

Here is my code

static void subtract()
        {

            bool tryagain = true;
            while (tryagain == true)
            {
                Console.WriteLine("Choose a range of numbers");

                Console.Write("First Value: ");
                int firstval = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("First value: "   firstval);

                Console.WriteLine("Second Value: ");
                int secondval = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Second value: "   secondval);
          
                Console.WriteLine(firstval   secondval);
                Random rnd = new Random();
                int firstrndval = rnd.Next(firstval, secondval); //this line is throwing the error
                int secondrndval = rnd.Next(firstval, secondval);
                Console.WriteLine("What is "   firstrndval   "-"   secondrndval);

                int potans;
                potans = Convert.ToInt32(Console.ReadLine());

                int ans = firstrndval - secondrndval;
                if (ans != potans)
                {
                    Console.WriteLine("Wrong Answer");
                }
                else
                {
                    Console.WriteLine("Correct Answer");
                }
                Console.WriteLine("Would you like to try again Y/N");
                string potbol = Console.ReadLine();

                potbol = potbol.ToUpper();

                if (potbol == "N")
                {
                    tryagain = false;
                }
            }
        }

I tried switching the values if the firstval is less than secondval but it would not work,i tried the tuple method and the three variable method but it didnt seem to work for me probably because it was in an if-else statement.I'm fairly new to C# and seem to not solve this problem.

CodePudding user response:

You have two options
First : If the first number is greater than the second , swap them like this

            if (firstval > secondval)
            {
                int temp = firstval;
                firstval = secondval;
                secondval = temp;
            }

this will ensure that the firstVal will always be the greater one
Second Solution : Use the Math.Min() & Math.Max() functions like this

int firstrndval = rnd.Next(Math.Min(firstval, secondval), Math.Max(firstval, secondval));
int secondrndval = rnd.Next(Math.Min(firstval, secondval), Math.Max(firstval, secondval));

this will ensure that the minimum value from both variables will be at first whatever if it's the firstVal or the SecondVal & the same for the maxmimum value from both variables

CodePudding user response:

First you must find the smaller number and the bigger number among the two:

int smaller = Math.Min(firstval, secondval);
int biggest = Math.Min(firstval, secondval);

int firstrndval = rnd.Next(smaller, biggest   1);
int secondrndval = rnd.Next(smaller, biggest   1);

You must put 1 on the second parameter because the second parameter is exclusive. That means, if you put rnd.Next(1,10) it will only return numbers among {1,2,3,4,5,6,7,8,9} (never 10).

  • Related