Bueno resulta que es super extraño, quiero decir que no esta terminado 100% el código pero debe de mostrar unos ciertos valores correctamente.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ejercicioMetodos
{
    class Program
    {
        static void Main(string[] args)
        {

            numeroIngresar();
            Console.ReadKey();
           
            
        }

        static void numeroIngresar() 
        {
            int cont = 0;
            int e = 0;
            Console.WriteLine("Cuantos numeros quieres ingresar: ");
            int numerosIngresar = int.Parse(Console.ReadLine());
            int[] numeroArray = new int[numerosIngresar];
            for (int i = 0; i < numerosIngresar; i++)
            {

                Random rnd1 = new Random();
                numeroArray[i] =rnd1.Next(5000) ;

            }
                while (e < numerosIngresar) 
                {
                   if(e+1 >= numerosIngresar)
                   {
                       break;
                   }
                   if(numeroArray[e]<numeroArray[e+1])
                   {
                       int auxiliar = numeroArray[e];
                       numeroArray[e] = numeroArray[e + 1];
                       numeroArray[e + 1] = auxiliar;
                   }
                   cont++;
                    if(cont == numerosIngresar)
                    {
                        break;
                    }
                    e++;
                }
            for(int j= 0 ; j < numerosIngresar;j++)
            {
            Console.WriteLine(numeroArray[j]);
            }
        }
        
    }
}
Explico lo que pasa, resulta que en estas líneas inicializo una NUEVA instancia del objeto Random:
for (int i = 0; i < numerosIngresar; i++)
            {

                Random rnd1 = new Random();
                numeroArray[i] =rnd1.Next(5000) ;

            }
Aparentemente no existe un fallo de sintaxis... pero claro cuando inicio la consola y le digo que genere 3 numeros ALEATORIOS pues sale 3 numeros igualitos..

Entonces estaba analizando el código y todo esta correctamente, entonces cogí y ejecute el DEPURADOR, y bueno la sorpresa es que cuando lo hago con el depurador, resulta que SI SALEN NUMEROS ALEATORIOS...

Entonces busco la ayuda de una mente experta en programación y que me diga por que pasa eso, me estoy volviendo loco
Se aprende viendo y aplicando Prueba, Error, Correccion
Saca la instancia del for..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ejercicioMetodos
{
    class Program
    {
        static void Main(string[] args)
        {
 
            numeroIngresar();
            Console.ReadKey();
            
             
        }
 
        static void numeroIngresar()
        {
            int cont = 0;
            int e = 0;
            Console.WriteLine("Cuantos numeros quieres ingresar: ");
            int numerosIngresar = int.Parse(Console.ReadLine());
            int[] numeroArray = new int[numerosIngresar];

            Random rnd1 = new Random();

            for (int i = 0; i < numerosIngresar; i++)
            {
 
                numeroArray[i] =rnd1.Next(5000) ;
 
            }
                while (e < numerosIngresar)
                {
                   if(e+1 >= numerosIngresar)
                   {
                       break;
                   }
                   if(numeroArray[e]<numeroArray[e+1])
                   {
                       int auxiliar = numeroArray[e];
                       numeroArray[e] = numeroArray[e + 1];
                       numeroArray[e + 1] = auxiliar;
                   }
                   cont++;
                    if(cont == numerosIngresar)
                    {
                        break;
                    }
                    e++;
                }
            for(int j= 0 ; j < numerosIngresar;j++)
            {
            Console.WriteLine(numeroArray[j]);
            }
        }
         
    }
}
De C# se nada. pero supongo que es porque creas la intancia cada vez en la repeticion.

si la crear fuera deberia funcionar.
Mira
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ejercicioMetodos
{
    class Program
    {
        static void Main(string[] args)
        {
 
            numeroIngresar();
            Console.ReadKey();
            
             
        }
 
        static void numeroIngresar()
        {
            int cont = 0;
            int e = 0;
            Console.WriteLine("Cuantos numeros quieres ingresar: ");
            int numerosIngresar = int.Parse(Console.ReadLine());
            int[] numeroArray = new int[numerosIngresar];
              Random rnd1 = new Random();
            for (int i = 0; i < numerosIngresar; i++)
            {
                numeroArray[i] =rnd1.Next(5000) ;
 
            }
                while (e < numerosIngresar)
                {
                   if(e+1 >= numerosIngresar)
                   {
                       break;
                   }
                   if(numeroArray[e]<numeroArray[e+1])
                   {
                       int auxiliar = numeroArray[e];
                       numeroArray[e] = numeroArray[e + 1];
                       numeroArray[e + 1] = auxiliar;
                   }
                   cont++;
                    if(cont == numerosIngresar)
                    {
                        break;
                    }
                    e++;
                }
            for(int j= 0 ; j < numerosIngresar;j++)
            {
            Console.WriteLine(numeroArray[j]);
            }
        }
         
    }
}


Saludos
Imagen
Va, va perfecto
Pero alguien sabe la causa de por que con el depurador si me la hace bien?
Muchas gracias
Se aprende viendo y aplicando Prueba, Error, Correccion
quizas el debugger no permite instanciar tantas veces.
sino que te lo instancia una sola vez lo que hace que funcione normalmente.
saludos
Imagen
Sanchez escribió:Va, va perfecto
Pero alguien sabe la causa de por que con el depurador si me la hace bien?
Muchas gracias
La clase Random se inicia usando el reloj, al ejecutarlo instancias la clase "X" veces en milisegundos y por esa razon siempre te devuelve el mismo valor, al debuggear tardas mas tiempo en repetir la linea y por eso te genera un valor diferente.
Imagen

Mostrar/Ocultar

Responder

Volver a “VB/.NET”