2

I've created a class called Superhero and given it the properties of: string Name, int Life, int Punch, int Kick, and int Hyper Punch.

Then inside of my program in the main I created a List.

static void Main(string[] args)
{
List<Superhero> listOfHeroes = new List<Superhero>;
Superhero goodGuy = new Superhero();
goodGuy.Name = "Super Man";
goodGuy.Life = 100;
goodGuy.Punch =????
}

I want to be able to put in a range of numbers where the "????" are. My theory is that a user would be able to choose a hero from the list and be able to use him for battle. When he chooses the hero to throw a punch (or a kick or whatever) then a random number within a set range is assigned to that property.

So if it was a two player turn based game the property punch, when used, would take away from the other person's life property by the value of Punch. Is this a possibility or am I going about this incorrectly?

pseudo-random is ok.

Btw, I'm in 4th week of boot camp...so yell at me harshly if none of this makes sense so I can learn quicker lol.

2
  • int value = Random.Next(min, max-1) Commented Mar 12, 2016 at 16:59
  • @Plutonix When I put that code in I get an error message of "an object reference is required for the non-static field, method, or property 'Random.Next(int,int)' " Is this because I'm not creating an instance of Random? Commented Mar 12, 2016 at 17:06

1 Answer 1

0

You need to initialize a Random instance using:

 Random rand = new Random(DateTime.Now);

Then you can use:

int value = rand.Next(min, max-1)

If you don't have a global initialization of Random instance, you will have always the same number using:

for (int i = 0; i < 10; i++)
{
   var t = (new Random()).Next(0, 100);
   Console.WriteLine(t);
}

Will return then time the same value

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.