Java Sum of Two Dice - will this code be 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 took this code from Sedgewick's book "Introduction to Java Programming" on my online site.

My question is only about whether a or b be higher than 6 if by chance Math.random() has 1.0 ? Or am I wrong about that?

1,0 * 6 + 1 = 7?

+5
source share
4 answers

Math.random() cannot return 1.0, so a or b cannot 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(); 
+3
source

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

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 math, you still need to consider if there is any value less than 1, so when multiplied by 6, the nearest represented double is 6, and not some value just below 6 ... but I I do not believe that the problem is here.

It would still be easier 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); } 
+2
source

The Math.random() method does not return 1.0, since it has its borders from 0.0 to 100, but does not include 1.0 and is multiplied by 6 and adds 1 to it, i.e. (Math.random()*6)+1 will return values ​​from 1 to 6 after entering the type (int) .

Variable parties could also be declared final

 private static int SIDES = 6; 
+1
source

random () can give values ​​close to 1, however they will always be LESS than 1. Here we are talking about casting to (int) from a positive double. The result will always be less than 6, changing in the set {0,1,2,3,4,5}. So the answer is NO, none, nor can there still be 7. I hope this helps.

To prove, run the following code:

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

Source: https://habr.com/ru/post/1207508/


All Articles