user2580104
user2580104

Reputation:

Java Sum Of Two Dice - Will This Code Give Above A 6?

public class SumOfTwoDice 
{ 
    public static void main(String[] args) 
    {
        int SIDES = 6;
        int a = 1 + (int) (Math.random() * SIDES);
        int b = 1 + (int) (Math.random() * SIDES);
        int sum = a + b;
        System.out.println(sum);
    }
}

I've taken this code from the book "Introduction to Programming with Java" by Sedgewick on their online website.

I just have a question as to whether a or b could possibly be above 6 if by chance Math.random() is 1.0? Or am I wrong on this?

1.0 * 6 + 1 = 7?

Upvotes: 5

Views: 278

Answers (4)

Cristian Botiza
Cristian Botiza

Reputation: 439

random() may produce values close enough to 1, however they will always be LESS than 1. The issue here is the cast to (int) from a positive double. The result will always be less than 6, varying in the set of {0,1,2,3,4,5}. So the answer is NO, neither a nor be can ever by 7. Hope this helps.

To prove, run the following code:

    double x=0.99;
    System.out.println((int)(6*x));
    
Play with the x value, no matter how close it is to 1, you will still get 5 or less printed.

Upvotes: 0

Orobo Lucky Ozoka
Orobo Lucky Ozoka

Reputation: 43

The Math.random() method doesn't return 1.0 since it has its bounds at 0.0 upto but not including 1.0 and multiplying by 6 and adding 1 to it i.e (Math.random()*6)+1 will return values from 1 to 6 after type-casted to (int).

Also the variable sides could have been declared as final

private static int SIDES = 6;

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1499840

No, Math.random() will never return 1. It has an inclusive lower bound of 0, but an exclusive upper bound of 1.0. From the documentation - emphasis mine:

Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.

Now given that this is floating point maths, you still need to consider whether there's some value less than 1 such that when multiplied by 6, the closest representable double is 6 rather than some value just below 6... but I don't believe that's a problem here.

It would still be clearer to use java.util.Random though....

private static final int SIDES = 6;

public static void main(String[] args) {
    Random random = new Random();
    int a = random.nextInt(SIDES) + 1;
    int b = random.nextInt(SIDES) + 1;
    int sum = a + b;
    System.out.println(sum);
}

Upvotes: 1

Eran
Eran

Reputation: 393771

Math.random() can't return 1.0, so a or b can't be 7.

/**
 * Returns a <code>double</code> value with a positive sign, greater 
 * than or equal to <code>0.0</code> and less than <code>1.0</code>.  <-----------
 * Returned values are chosen pseudorandomly with (approximately) 
 * uniform distribution from that range. 
 * 
 * <p>When this method is first called, it creates a single new
 * pseudorandom-number generator, exactly as if by the expression
 * <blockquote><pre>new java.util.Random</pre></blockquote> This
 * new pseudorandom-number generator is used thereafter for all
 * calls to this method and is used nowhere else.
 * 
 * <p>This method is properly synchronized to allow correct use by
 * more than one thread. However, if many threads need to generate
 * pseudorandom numbers at a great rate, it may reduce contention
 * for each thread to have its own pseudorandom-number generator.
 *  
 * @return  a pseudorandom <code>double</code> greater than or equal 
 * to <code>0.0</code> and less than <code>1.0</code>.
 * @see     java.util.Random#nextDouble()
 */
public static double random();

Upvotes: 2

Related Questions