Generating 10 digits unique random number in java -


I'm trying to generate a unique random number of 10 digits, with the code below. According to my request, I have to make about 5000 unique numbers (id). It is not working as expected, it also produces -ve number, sometimes sometimes a or two digit disappears in a number, resulting in 8 or 9 numbers are not there. 10.

  public stable synchronized list generated originsandpin () {int START = 1000000000; // int eden = integer.center ("9999999999"); // long END = integer.center ("9999999999"); Long END = 99 99 99 99 99 L; Random random = new random (); {CreateRandomInteger (start, end, random) for (id idx = 1; idx and lt; = 3000; ++ idx); } Return tap; } Random Integer (Int octart, Long A & R, Random AR & M) {to (aStart> anEnd) {To establish new invalid records ("Start can not end.") To create personal static void; } // Get the range, long-term casting to avoid overflow problems Long term = (long) ends - (long) aStart + 1; Logger.info ("borders> gt ::>> gt;> gt ::> gt ::> gt;>> gt; & gt;" + range); // Calculate a fraction of the range, 0 < = Frac & lt; Limit long degrees = (long) (range * aRandom.nextDouble ()); Logger.info ("parts> gt>>> gt>> gt>> gt;> gt;> gt;> gt;> gt;> gt; & gt; & gt; & gt; & gt; |>> Gt;> gt; & gt; "+ fraction); Int randomNumber = (int) (excerpt + aStart); Logger.info ("generated:" + random number); }    

I think you get 8/9 digit values ​​and negative numbers It is that you are adding fraction , a long (sign of 64-bit value) that is positive int range (32-bit value) ) To aStart .

The value is overflowing, such that random number is in negative 32-bit range or about AStart (because int There is a signed 32-bit value, to see the fraction only (2 ^ 32 - aStart 8 or 9 digits value)

You have to use long for all the values.

  Random integer (IntAttert, Long Aid, Random AR & M) to create a personal static void {if (aStart> anEnd) {setting new invalid records ("Start can not end. "); } // Get the range, long-term casting to avoid overflow problems Long period = Eed - (long) aStart + 1; Logger.info ("borders> gt ::>> gt;> gt ::> gt ::> gt;>> gt; & gt;" + range); // Calculate a fraction of the range, 0 < = Frac & lt; Limit long degrees = (long) (range * aRandom.nextDouble ()); Logger.info ("parts> gt>>> gt>> gt>> gt;> gt;> gt;> gt;> gt;> gt; & gt; & gt; & gt; & gt; |>> Gt;> gt; & gt; "+ fraction); Long random number = fraction + (tall) aStart; Logger.info ("generated:" + random number); }    

Comments