Random generates the same result several times

0
string nombre;
    public string gen2(int Name_Len)
    {
        string[] abc = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };

        if (Name_Len >= 5)
        {
            for(int c = 0; c <= Name_Len; c++)
            {
                Random rand = new Random();
                nombre += abc[rand.Next(0, 24)];
            }
        }
        return nombre;
    }
 gen2(6);

This is happening to me as a result of EJ: JJJJJJ || KKKKKK ... etc. and in each execution, each element is repeated, that is to say, it is very probable that I will come out again JJJJJJ, KKKKKK, I had read something that Random generates as a function of pc time, something like that. Excuse my query.

    
asked by Berishten VanCheese 06.08.2017 в 20:09
source

1 answer

3

I already solved it, the problem is that I created the object within the method, I only had to do this:

string nombre;
Random rand = new Random();//Fuera del Método
public string gen2(int Name_Len)
{
    string[] abc = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };

    if (Name_Len >= 5)
    {
        for(int c = 0; c <= Name_Len; c++)
        {
            //aqui estaba el Método
            nombre += abc[rand.Next(0, 24)];
        }
    }
    return nombre;
}

gen2 (6);

In response if someone else happens;)

    
answered by 06.08.2017 / 20:21
source