I am trying to write a method rollDice(int number, int nSides) which returns the total result of rolling the number dice with nSides sides.
So for example rollDice(3
Random.nextInt()
has unpredicable behaviour - it can produce all values possible for an integer, including negative numbers. Use Random.nextInt(numSides)
instead - it will return an integer from [0,numSides) i.e. including 0 and excluding numSides. To get your desired functionality [1,numSides] use
r.nextInt(numSides)+1;
See here for more information.