Random Function Properly [closed]

0

As I can use the function Random in C # using two functions, the first function is the minimum damage of the player, and the second the maximum damage that a player can do, my idea is to use the function Random , calling to both functions min. and max. and based on the range of these shows the random number, however it does not work, I attach my code that is a console function.

PS: I left the code that does not work commented.

using System;

namespace ConsoleApp2
{
    class Program
    {   


        //Metodos para devolver un valor

        static int DanoMinMeleeDistance(int STR, int DEX, int WeaponATK)
        {
            int DanoMinMeleeDistance = ((STR+DEX)/2)+WeaponATK;
            return DanoMinMeleeDistance;
        }
        static int DanoMaxMelee(int STR, int DEX, int WeaponATK)
        {
            int DanoMaxMelee = STR+WeaponATK+(DEX/2);
            return DanoMaxMelee;
        }

       /* static int DanoAleatorio(Random rnd, int DanoMaxMelee, int DanoMinMeleeDistance) {

            int DanoAleatorio = rnd.Next(DanoMinMeleeDistance, DanoMaxMelee);
            return DanoAleatorio;


        }*/

        // Main el principal
        static void Main(string[] args)
        {

            int STR, DEX, WeaponATK;
            Console.Write("INGRESE PRIMER VALOR: ");
            STR = int.Parse(Console.ReadLine());
            Console.Write("INGRESE SEGUNDO VALOR: ");
            DEX = int.Parse(Console.ReadLine());
            Console.Write("INGRESE TERCER VALOR: ");
            WeaponATK = int.Parse(Console.ReadLine());

            Console.Write("1º) Daño Minimo" + "\n" + "2º) Daño Maximo" + "\n" + "3º) Daño Aleatorio <strong>\n");
            Console.Write("Seleccione una opción: ");



            switch (Console.Read())
            {
                case '1':
                    Console.Write("Daño Minimo = " + DanoMinMeleeDistance(STR, DEX, WeaponATK));
                    // Continuar lógica y extraer métodos //
                    break;
                case '2':
                    Console.Write("Daño Maximo = " + DanoMaxMelee(STR, DEX, WeaponATK));
                    // Continuar lógica y extraer métodos // 
                    break;

                /*case '3':
                    Console.Write("El numero aleatorio es" + DanoAleatorio()*/
            }
            Console.ReadKey(); // Un pausee para presionar la tecla
        }
    }


}
    
asked by Christian Andres Hyuden 20.09.2018 в 17:34
source

2 answers

1

The error in your code is in what you call your 'DanoAleatorio' method. If you see well, your method receives 3 parameters (Random rnd, int DanoMaxMelee, int DanoMinMeleeDistance) , but you are calling the function without any parameters and this causes the error ...

On the other hand, you should define which Random to use, for my experience only use 2, look at the following code

Example of how to use random of a static class:

public int DanoAleatorio(int min, int max)
{
   return Random.Range(min, max); //Metodo estatico
}

 //Como usarlo
 int numero = DanoAleatorio(123, 456);

Link to this random

Another way to make random numbers, creating an instance of the class:

Random random = new Random(); //Este es el que trae por defecto .net, y se necesita una instancia para generar numeros
int randomNumber = random.Next(0, 100);

For more information on functions and methods, see this link

I hope I have helped you!

Edit:

The Random function is a pseudo-random function (you can find info here on the site, there are several questions about it). When you define a random type object, it is initialized with a value. When you do the next, you can tell it between what values you want the next number to return, passing it a minimum and a maximum:

random.Next(min,max);

What you are doing in your code is fine, except that you are forgetting to pass an object of the random type. Before your call, add:

Random rnd = new Random();

and then make your call in the following way:

DanoAleatorio(rnd,DanoMinMeleeDistance(STR, DEX, WeaponATK),DanoMaxMelee(STR, DEX, WeaponATK))

or to make it look better:

Random rnd = new Random();
int min = DanoMinMeleeDistance(STR, DEX, WeaponATK);
int max = DanoMaxMelee(STR, DEX, WeaponATK);
DanoAleatorio(rnd,min,max);

Thanks @gbianchi for the editing and collaboration!

    
answered by 20.09.2018 в 18:55
0

I solved it I will show step by step how I solved it thanks @gbianchi for patience now I explain how I solved it. I added, a method called Constants, here I gave a value to each Variable. Then I added the Method class, where all the calculations go:

At the end I added the program class which proceeds to the call and execution of the objects that I declare in this same class:

And at the end it works, without errors:

Thank you all: 3

    
answered by 21.09.2018 в 16:35