0
/**
 * @return random damage from monster
 * @param monster of class Monster
 */
public int getRandomDamage(Monster monster) {
    int min = monster.getMinDamage(); //this is set to 20 for monsters
    int max = monster.getMaxDamage(); //this is set to 30 for monsters

    return randomGenerator.nextInt(max - min + 1) + min;
}

This return statement don't work, since it always returns the integer 20. Any tip for solving, it should return a random number between 20(int min) and 30(int max). Thx for any help.

After some problem solving turned out that getMaxDamage(); returned 20 and not 30, runs good now, thou stupid typing error.

user3408091
  • 191
  • 1
  • 6
  • 3
    That general equation will work. I suspect that your values are *not* what you expect (either that or you are re-seeding the PRNG). – user2864740 Mar 11 '14 at 21:45
  • Are you certain `getMinDamage()` and `getMaxDamage()` are returning what you expect? Because this code looks fine. – Jason C Mar 11 '14 at 21:45
  • 4
    can you please provide how you are constructing randomGenerator? – hmashlah Mar 11 '14 at 21:46
  • possible duplicate of [Generating random numbers in a range with Java](http://stackoverflow.com/questions/363681/generating-random-numbers-in-a-range-with-java) – MGot90 Mar 11 '14 at 21:47
  • 1
    @MikeG010590 The OP already seems to have the technique for generating random numbers down fine. The only real explanation for the behavior he is describing is that `getMaxDamage()` is returning 20 instead of 30. – Jason C Mar 11 '14 at 21:50
  • Yeah, insert a println for your min and max values before the nextInt call, and make sure you only create `randomGenerator` *once*, at the start of your program. – Hot Licks Mar 11 '14 at 21:51
  • yeah, there was a typing error in getMaxDamage(); it returned 20, but now it is 30. bad mistake – user3408091 Mar 11 '14 at 22:07

2 Answers2

4

The code posted is a valid and accepted approach (although, do re-use the same PRNG..).

The most plausible explanation of the behavior that I can think of, is that the values are not what are expected. For instance, the following will always yield 20.

int min = 20;
int max = 20;
return randomGenerator.nextInt(max - min + 1) + min;

Replacing the values yields nextInt(1) + 20, where nextInt(1) always returns 0 as Random.nextInt(n) returns an integer in the range [0,n).


Some other causes .. (debugging is about developing and testing hypothesis)

Perhaps, randomGenerator is not Random and is just broken.

class FairRandom {
    public int nextInt(int n) {
        return 0; /* chosen by fair dice roll */ }
    }
}

Or, perhaps, a random number is being generated correctly, and the "always 20" value comes from elsewhere.

Or, perhaps, the random number generator is being re-seeded before each call to nextInt and you are very unlucky that the seed results in 20 being chosen "randomly".

Or, perhaps, you're listening to @JasonC and playing havoc with all my assumptions ;-)

Community
  • 1
  • 1
user2864740
  • 60,010
  • 15
  • 145
  • 220
  • 1
    The first example would actually return 20 all the time, [`nextInt` chooses between min (inclusive) and max (exclusive)](http://docs.oracle.com/javase/7/docs/api/java/util/Random.html#nextInt(int)) -- `nextInt(1)` will always return 0. But, +1! – Jason C Mar 11 '14 at 21:57
  • 1
    The OP could also be using e.g. javassist to instrument the byte code and replace his method, or `Random.nextInt()`, with a method that always returns 20. 8-) – Jason C Mar 11 '14 at 22:01
  • @JasonC I can only hope not - but I've seen some strange things :| – user2864740 Mar 11 '14 at 22:01
1

Your random number generation code is fine.

The most reasonable explanation for what you are seeing is that getMaxDamage() is actually returning 20 instead of 30.

I suggest adding some temporary print-outs to inspect those values, or running it in a debugger, as well as examining the getMaxDamage() code for errors.

Other explanations include an extremely unlucky presence of neutrinos / cosmic rays causing bit flips in CPU registers that happen to result in 20 being calculated every time, or perhaps the Intergalactic Council of Zero Entropy deemed your actions to have too high a risk of exposing the usefulness of random numbers and overwrote your memory of debugging your software with a memory of seeing 20 to help their larger goal of spreading anti-random propaganda throughout the universe.

Jason C
  • 38,729
  • 14
  • 126
  • 182
  • The most probable reason is not in getMinDamage and getMaxDamage, but rather in broken random numbers generator. – dnl-blkv Mar 11 '14 at 22:00
  • 1
    @Danek That is another possibility but I don't see any evidence that supports the conclusion that it is more likely; personally it seems more likely that the methods are returning incorrect values given that the OPs presumed skill level doesn't suggest the implementation of a custom random number generator. – Jason C Mar 11 '14 at 22:08