jeudi 24 septembre 2015

C# random numbers in a for loop [duplicate]

This question already has an answer here:

I would like to get random numbers in my c# project. The issue is, that the random number won't change.

    public PersonenListe()
    {
        List<Personen> personen = new List<Personen>();
        for (int i = 1; i < 10; i++)
        {
            Random rnd = new Random();
            int vorname = rnd.Next(Personen.vornamen.Count());
            int nachname = rnd.Next(Personen.vornamen.Count());

            Personen person = new Personen(Personen.vornamen[vorname], Personen.nachnamen[nachname]);

            personen.Add(person);
        }
    }

Here is a array with some names with each 20 names (prename, surname)

    public static string[] vornamen =
    {
        "Hans",
        "Sepp",
        "Johann",
        ...
    };

    public static string[] nachnamen =
    {
        "Müller",
        "Köchli",
        "Bütler",
        ...
    };

I would like to get diffrent names in each person. If i do a "Console.WriteLine(p.Vorname + p.Nachname);" there is just the same name!

Thanks




Aucun commentaire:

Enregistrer un commentaire