samedi 22 octobre 2016

C#, generating random new objects

I am developping a small game to learn C# in a more fun way.

Im trying to make a method that generates a random Monster based on rarity. The methods returns a random monster but 5 times the same even tough I call it 5 different time.

This is my method that generates a random monster

    public Pokemium RandomPokemium()
    {
        Random random = new Random();
        int rarete = random.Next(0, 100);

        if (rarete >= 0 && rarete <= 25)
        {
            switch (random.Next(1, 4))
            {
                case 1:
                    Leggs legg = new Leggs();

                    return legg;

                case 2:
                    Milkow milkow = new Milkow();
                    return milkow;
                case 3:
                    Canneberge canneberge = new Canneberge();
                    return canneberge;
            }
        }
        else if (rarete > 25 && rarete <= 45)
        {
            switch (random.Next(1, 5))
            {
                case 1:
                    Proturkey proturkey = new Proturkey();
                    return proturkey;
                case 2:
                    Lactensien lactensien = new Lactensien();
                    return lactensien;
                case 3:
                    Dildough dildough = new Dildough();
                    return dildough;
                case 4:
                    Kungfish kungfish = new Kungfish();
                    return kungfish;
            }

        }
        else if (rarete > 45 && rarete <= 60)
        {
            switch (random.Next(1, 4))
            {
                case 1:
                    Swordster swordster = new Swordster();
                    return swordster;
                case 2:
                    Fusilli fusilli = new Fusilli();
                    return fusilli;
                case 3:
                    Caparrot caparrot = new Caparrot();
                    return caparrot;
            }
        }
        else if (rarete > 60 && rarete <= 70)
        {
            switch (random.Next(1, 4))
            {
                case 1:
                    Katanana katanana = new Katanana();
                    return katanana;
                case 2:
                    Fawnter fawnter = new Fawnter();
                    return fawnter;
                case 3:
                    Crabtyson crabtyson = new Crabtyson();
                    return crabtyson;
            }
        }
        else
        {
            switch (random.Next(1, 4))
            {
                case 1:
                    Emental emental = new Emental();
                    return emental;
                case 2:
                    Rapghetti rapghetti = new Rapghetti();
                    return rapghetti;
                case 3:
                    Masturbaker masturbaker = new Masturbaker();
                    return masturbaker;
            }
        }
        Leggs leggs = new Leggs();
        return leggs;
    }

This is my method that generates 5 random monsters and put them in a generic list.

    public List<Pokemium> GenererEquipeEnnemis(int level)
    {
        Pokemium poke = RandomPokemium();
        Pokemium poke1 = RandomPokemium();
        Pokemium poke2 = RandomPokemium();
        Pokemium poke3 = RandomPokemium();
        Pokemium poke4 = RandomPokemium();

        List<Pokemium> ennemis = new List<Pokemium>();
        ennemis.Add(poke);
        ennemis.Add(poke1);
        ennemis.Add(poke2);
        ennemis.Add(poke3);
        ennemis.Add(poke4);

        return ennemis;
    }

Everytime I run the program I get a different monster but 5 times the same. Is there a reason why I dont get 5 random monsters?

Exemple first time I run the program, ennemis would have Leggs Leggs Leggs Leggs Leggs

and another time it would be a different monster but 5 times.




Aucun commentaire:

Enregistrer un commentaire