2

I'm making a game in Java where I need dices. There are 3 dices for the attacker and 2 dices for the defencer. This is my code that randomize the eyes on all dices:

        if (attacker.getArmies() > 1)
            aDices[0] = random.nextInt(6) + 1;
        if (attacker.getArmies() > 2)
            aDices[1] = random.nextInt(6) + 1;
        if (attacker.getArmies() > 3)
            aDices[2] = random.nextInt(6) + 1;
        if (defencer.getArmies() > 0)
            dDices[0] = random.nextInt(6) + 1;
        if (defencer.getArmies() > 1)
            dDices[1] = random.nextInt(6) + 1;

But why are the numbers of the 'defence' dices mostly higher then the 'attack' dices?

I'm using the Random class from java.util.Random

13
  • 3
    Can you substantiate your claim? Commented Oct 13, 2012 at 13:11
  • 1
    How many times do you mean my "mostly higher"? Commented Oct 13, 2012 at 13:11
  • 1
    do you seed your random number generator? Commented Oct 13, 2012 at 13:12
  • 4
    See dilbert.com/strips/comic/2001-10-25 Commented Oct 13, 2012 at 13:12
  • 2
    I know which Dilbert that is without even looking ;-) Commented Oct 13, 2012 at 13:17

3 Answers 3

3

Seeing that you are using exactly the same function for attack and defense, it's simply impossible that the outcome is structurally higher for attackers. What you could try to convince yourself is write a little function that rolls the dice a 100 times as attack and defense and calculate averages for that. And even then it is still possible that after a 100 times, attack wins a 100 times, unlikely, but possible. That's the nature of randomness.

This is a nice short read on randomness for some background information:

http://engineering.mit.edu/live/news/1753-can-a-computer-generate-a-truly-random-number

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

Comments

0

Maybe you should use the Random class in combination with timestamps from your PC clock. It's called a seed for the randomizer.

Comments

0

It would seem that the code you're showing is giving more of an advantage to the defender than original Risk does;

if (attacker.getArmies() > 1)          // 1 army will never roll the dice
    aDices[0] = random.nextInt(6) + 1;
  • The attacker gets 1 dice roll per army (1-3) he attacks with. You're giving him 0-2. That would mean that an attack with one army will never succeed.
  • The defender gets 1 dice roll per army (1-2) he defends with. You're giving him 1-2.

That would tilt the game more towards the defender than original risk does, all without getting biased random numbers.

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.