Open In App

How to Generate Unique Positive Long Number in Java?

Improve
Improve
Like Article
Like
Save
Share
Report

Java provides different ways to generate random numbers, using some built-in methods and classes, but most of them do generate unique positive long numbers like java.util.Random class, Math.random method and ThreadLocalRandom class, and many others. But in most of these either with randomness uniqueness is not followed along which means a random number generated by those methods is not unique, this can be verified by providing a small range to Random().

Example 1:

Java




// Java Program to generate Random Number Between 1-4 using
// random() function
import java.io.*;
import java.util.Random;
 
class GFG {
    public static void main(String[] args)
    {
        Random random = new Random();
        // initialize variable rand with 0
        int rand = 0;
        while (true) {
            // rand will store one of the random value
            // between 0-4 described using nextInt(5) below
            // in random.nextInt(5)
            rand = random.nextInt(5);
            if (rand != 0)
                break;
        }
        System.out.println(rand);
    }
}


Output

3

Example 2:

Java




// Java Program to generate random double numbers using
// random() function
import java.io.*;
import java.util.Random;
 
class GFG {
    public static void main(String[] args)
    {
        Random random = new Random();
 
        // to get a random and most likely unique double
        // value use nextDouble() with random
        double d = random.nextDouble();
        System.out.println(d);
 
        double d1 = Math.random();
        System.out.println(d1);
    }
}


Output

0.48783050588815524
0.05301287078228778

Few other libraries which are actually capable to produce unique number come with the drawback that the output number include both positive and negative number, here uniqueness might be followed but if the requirement is aligned towards producing a positive unique number, these libraries will not be useful

To produce a positive unique(extremely low chances of repetition) long value in java the well-known UUID library which is also widely used for generating unique strings, using UUID unique long number of the desired length can be obtained as below

UUID.randomUUID().toString() produces a unique String of length 36 and is made up of characters (alphabets and “-“) and digits.

Example:

Java




// Java Program To use UUID to generate long positive values with BigInteger
import java.math.BigInteger;
import java.util.UUID;
class GFG {
    public static void main(String[] args)
    {
        // The UUID.randomUUID().toString() of length
        // consist of digits ,alphabets which will be handled
        // to get digits using BigInteger and "-" which needs
        // to be replaced with "". Inside  new
        // BigInteger("%010d", new
        // BigInteger(UUID.randomUUID().toString().replace("-",
        // ""), 16)) 16 represent radix .
        String generateUUIDNo = String.format("%010d",new BigInteger(UUID.randomUUID().toString().replace("-",""),16));
 
        // To decide length of unique positive long number
        // generateUUIDNo.length() - uniqueNoSize is being
        // used
        String unique_no = generateUUIDNo.substring( generateUUIDNo.length() - 10);
        System.out.println(unique_no);
    }
}


Output

9918386712

where uniqueNoSize represents the size of the unique number required, it should be less than equal to 38 as generateUUIDNo length is 38.



Last Updated : 05 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads