Home > Mobile >  Best way to generate unique Random number in Java
Best way to generate unique Random number in Java

Time:09-13

I have to generate unique serial numbers for users consisting of 12 to 13 digits. I want to use random number generator in Java giving the system. Time in milliseconds as a seed to it. Please let me know about the best practice for doing so. What I did was like this

Random serialNo = new Random(System.currentTimeMillis());
System.out.println("serial number is " serialNo);

Output came out as: serial number is java.util.Random@13d8cc98

CodePudding user response:

Create a random number generator using the current time as seed (as you did)

long seed = System.currentTimeMillis();
Random rng = new Random​(seed);

Now, to get a number, you have to use the generator, rng is NOT a number.

long number = rng.nextLong();

According to the documentation, this will give you a pseudorandom number with 281.474.976.710.656 different possible values.

Now, to get a number with a maximum of 13 digits:

long number = rng.nextLong() % 10000000000000;

And to get a number with exactly 13 digits:

long number = (rng.nextLong() % 9000000000000)   1000000000000;

CodePudding user response:

For a bit better algorithm pick SecureRandom. You passed a seed to the random constructor. This will pick a fixed sequence with that number. A hacker knowing the approximate time of calling, might restrict the number of attempts. So another measure is not using the constructor and nextLong at the same spot.

SecureRandom random = new SecureRandom​();
long n = random.nextLong();

A symmetric bit operation might help:

n ^= System.currentMillis();

However there is a unique number generation, the UUID, a unique 128 bits number, two longs. If you xor them (^) the number no longer is that unique, but might still be better having mentioned the circumstantial usage of random numbers.

UUID id = UUID.randomUUID();
long n = id.getLeastSignificantBits() ^ id.getMostSignificantBits();

CodePudding user response:

Random random = new Random(System.currentTimeMills()); but i don't know this code

CodePudding user response:

First, import the Random class:

import java.util.Random;

Then create an instance of this class, with the current milliseconds as its seed:

Random rng = new Random(System.currentTimeMillis());

This line would generate an integer that can have up to 13 digits:

long result = rng.nextLong() % 10000000000000;

This line would generate an integer that always have 13 digits:

long result = rng.nextLong() % 9000000000000   1000000000000;

CodePudding user response:

There are three ways to generate Random numbers in java

  1. java.util.Random class We can generate random numbers of types integers, float, double, long, booleans using this class.

Example : //Random rand = new Random(); // rand_int1 = rand.nextInt(1000)

  1. Math.random method : Can Generate Random Numbers of double type. random(), this method returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.

Example : Math.random()); //Gives output 0.15089348615777683

  1. ThreadLocalRandom class This class is introduced in java 1.7 to generate random numbers of type integers, doubles, booleans etc

Example :

    //int random_int1 = ThreadLocalRandom.current().nextInt();
    // Print random integers
    //System.out.println("Random Integers: "   random_int1);
   
  • Related