-2

I stopped programming a few years ago because I didn't think I would get to graduate. Surprise, now I am, and I have to brush up on rusty novice skills. I decided to start with a dice rolling program written in Java. The first version of this program is just supposed to spit out a random number between 1 and 10. As I go I plan to expand it with a GUI that allows the user to select the number and type of dice to roll.

The first version of this program works. My problem is that I don't know why it works. I was first going to use java.util.random and use random.nextInt() but the range was enormous and I couldn't figure out how to limit it to a specific range.

I searched online and found something that worked. Here's what I have.

class DiceRoller {

    public static void main(String[] arguments){

        int min = 1;
        int max;
        int result;

        result = rollDice(10, min);

        System.out.println(result);
    }

    public static int rollDice(int maximum, int minimum){
        return ((int) (Math.random()*(maximum - minimum))) + minimum;
    }
}

The specific part that I found to make this work is:

    public static int rollDice(int maximum, int minimum){
        return ((int) (Math.random()*(maximum - minimum))) + minimum;
    }

But I don't understand why it works. Is it something with random() within the Math class itself that differs from Random in java.util? Why is it necessary to take the random number and multiply it by the difference of the maximum and minimum, then add that result to the minimum? To be honest I feel stupid for asking but I want to understand why this works rather than just take the code and use it because it does.

Edit: I know.. I know.. I should've just read the class documentation. Thanks for humoring me and answering anyway. I appreciate the replies, and thank you Aaron for the explanation.

TK11612
  • 3
  • 3
  • 3
    read this question http://stackoverflow.com/questions/363681/generating-random-integers-in-a-specific-range.some answers describe how it works.read this answer http://stackoverflow.com/a/363732/2227526 – Madhawa Priyashantha May 15 '16 at 13:24
  • Math.random returns a random double between 0.0 and 1.0. Work it out on paper what this equation does,.... think it through. – Hovercraft Full Of Eels May 15 '16 at 13:26
  • The official JavaDocs are always a good source of information. There you find the differences between `Random` and `Math.random()` and of course the docs of every class and method. Link: https://docs.oracle.com/javase/8/docs/api/ – Michael May 15 '16 at 13:32
  • You found [`Random.nextInt()`](https://docs.oracle.com/javase/8/docs/api/java/util/Random.html#nextInt--), which is documented to return an integer in the range `[Integer.MIN_VALUE, Integer.MAX_VALUE]`. There is another overload, [`Random.nextInt(int)`](https://docs.oracle.com/javase/8/docs/api/java/util/Random.html#nextInt-int-) which returns an integer in the range `[0, bound)`. See if you can't use that method instead. – dimo414 May 15 '16 at 13:32

2 Answers2

1

Math.random() returns a random number between 0 and 1.
So if you want a random number between 0 and n instead, you just have to multiply by n.
Now if you want a number between m and n, you will want to do what is done there : multiply by n - m, then add m.

Example with a roll between 2 and 5 :

  • I've got my random number between 0 and 1
  • I multiply it by 5 - 2 = 3. Now I've got a random number between 0 and 3
  • I add 2. Now I've got a random number between 2 and 5
Aaron
  • 24,009
  • 2
  • 33
  • 57
0
return ((int) (Math.random()*(maximum - minimum))) + minimum;

This has 4 parts (in execution order): Math.random() makes a number between 0 and 1.

*(maximum-minimim) gets the range (distance between) max. and min. then it scales the random number to that range. Eg. 0.5*(100-0) = 50, 0*(100-0) = 0, 0.99*(100-0) = 99. Not that the lowest value here is 0 and the highest is max. - min.

(int) Takes the number you have, somewhere between 0 and your range, and rounds it DOWN as it turns the number into an int (I always use either Math.round(), .floor(), or .ceil() so I was actually surprised by that.

+ minimum makes sure that the lowest possible value (which was 0 until now) is minimum by shifting the number up by that much. (Remember that we subtracted minimum earlier) The highest possible value is now maximum.