Home>

-Create a method that returns a random value from the range of the upper limit value and lower limit value as arguments
(However, don't specify an argument to the Random.Next method)

I ’m experiencing issues

Range random number formula: Minimum value + (int) (Random value * (Maximum value-Minimum value + 1.0)/(1.0 + Maximum random number value))
I was told to do it, but for some reason the minimum value is reflected
Applicable source code
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CS1303_C
{
    class Program
    {
        static void Main (string [] args)
        {
            Console.Write ("Lower limit:");
            int a = int.Parse (Console.ReadLine ());
            Console.Write ("Upper limit:");
            int b = int.Parse (Console.ReadLine ());
            int sum = Add (a, b);
            Console.Write (sum);
        }
        static int Add (int x, int y)
        {
            Random rnd = new Random ();
            int z = rnd.Next (x, y);
            int num = x + (int) (z * (y-x + 1.0)/(1.0 + int.MaxValue));
            return num;
        }
    }
} #

I felt that int.MaxValue was wrong, so I checked it and got an error

Supplemental information (FW/tool version etc.)

Microsoft VisualStudio2017

  • Answer # 1

      

    However, don't specify an argument to the Random.Next method

    int z = rnd.Next (x, y);// specify

  • Answer # 2

    int z = rnd.Next (x , y);
    If you remove the x and y values ​​of

  • Answer # 3

    If it is an assignment, it would be helpful to ask the teacher or teacher.

      

    The minimum value is reflected for some reason

    That's a little unclear

Related articles