dimanche 25 janvier 2015

Last 3 digits always zero when generating 15-digit random number

I wanted to generate a random number between 1_000_0000_0000_000 and 9_999_9999_9999_999Lusing Java so I used the following code,



final long MAX_NUMBER = 9_999_9999_9999_999L;
final long MIN_NUMBER = 1_000_0000_0000_000L;
System.out.print(Long.valueOf(new Random().nextLong() *
(MAX_NUMBER - MIN_NUMBER)));


The problem with the code is, the code does generate 15 digit random number, but the last 3-digits are always zero, and some time it generates negative number.


Can anybody tell me why the last 3-digits are always zero ?


Here are some of the outputs,


7160629848698886104000


-6581075034034719564000


6645937379553681443000


6363833355925386903000





Aucun commentaire:

Enregistrer un commentaire