-2

I'm creating a simple game (Yatzee) in C#. I have created an array which holds 5 different dice int[] dice = new int[5];

Now, I want to create a method that throw one of these five dices. Which die that should be thrown, should be passed in as an argument in that method. So this is how I tried:

public void throwDice(int x)
{
    Random r1 = new Random(6);
    r1.x;
}

What I believe is happening, is that the method takes in an argument x, that randomly should throw the dice to be a number between 1-6. But I'm getting error with when I write saying : r1.x;

So, why I'm asking here, is if I could get some guidance. Am I on the right track here, or am I totally lost?

Jesse C. Slicer
  • 19,901
  • 3
  • 68
  • 87
  • 1
    I've never heard of `x` field/property on `Random` class. Have you even tried compiling your code? – MarcinJuraszek Jan 11 '14 at 19:48
  • 1
    I think you're missing the basics, and not just of `Random`. Read the following link it should give you a good start http://stackoverflow.com/questions/2706500/how-to-generate-random-int-number-c – Orel Eraki Jan 11 '14 at 19:48
  • 2
    whenever somewhere in the world a game development homework is given, SO gets full of low quality questions about *Rock Paper Scissors* *Tic Tac Toe* *Yatzee* etc. I hate those times :( – L.B Jan 11 '14 at 19:55

3 Answers3

5

You are using the Random object wrong. The constructor parameter is a seed. You need r1.Next(6)+1.

See the related post for details: How do I generate a random int number in C#?.

What you probably want to do is this:

Random rnd = new Random();
int[] dice = new int[5];

void ThrowDie(int x)
{
    dice[x] = rnd.Next(6)+1;
}
Community
  • 1
  • 1
Igor Ševo
  • 5,459
  • 3
  • 35
  • 80
1

r1 is an instance of a Random class. x is a parameter of your throwDice method.

That does not mean your r1 must have a field called x.

I think you are looking for something like;

Random r1 = new Random();
public int throwDice()
{
  return r1.Next(1, 6);
}

Rememer, in Random.Next method lowerbound is inclusive but upperbound is exclusive.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • 1
    And that `Random` instance should not be in the method. It should either be a parameter or created at the class level. There could be repeat issues if that method were called repeatedly quickly. – Jesse C. Slicer Jan 11 '14 at 19:56
  • @JesseC.Slicer Sure, using `Random` instance repeatedly can generating the same values.. – Soner Gönül Jan 11 '14 at 20:02
0

Here is another approach to this matter:

public class RandomDice
{
   private const int NUMBER_OF_DICE = 5;
   private const int MAX_DICE_VALUE = 6;
   private static readonly Random Rng = new Random();
   public List<int> NumberList = new List<int>();

   private static IEnumerable<int> GetRandomNumbers()
   {
      while (true)
      yield return Rng.Next(1, MAX_DICE_VALUE + 1);
   }

   internal void AddDice()
   {
      NumberList.Clear();
      NumberList.AddRange(GetRandomNumbers().Take(NUMBER_OF_DICE).ToList());
   }
}
Gabriel Marius Popescu
  • 2,016
  • 2
  • 20
  • 22