mardi 29 mars 2016

random number in constructor c#

I am a student taking an introductory programming course in line with game development. One of my assignments calls for me to define a players attack damage inside of the constructor. I want the damage to be random, but no matter what I do I get back the same number. This is a test I made to see how I can get this number to be random.

class MainChar
{
    public static Random random = new Random();
    public int Atk;        

    public MainChar()
    {
        this.Atk = random.Next(11);
    }

    public void h()
    {
        while (Atk != 10)
        {
            Console.WriteLine(Atk);

        }
    }
}

I'm creating a new instance of MainChar in my main program, and running h(). I get a repeating list of the same number instead of random numbers between 1 and 10. Any ideas?

P.S. This was useful, but could not answer my question.




Aucun commentaire:

Enregistrer un commentaire