Sunday, October 14, 2018

How do you write an algorithm to generate unique 17 digit number in java ?

Problem statement:
write an algorithm to generate 17 digit unique number.

public class GenerateUniqueRandNums {
    public static void main(String args[]) {
        for (int count = 0; count < 1000000; count++) {
            long unqNum = System.nanoTime();
            System.out.println("unique: " + unqNum* 100);
        }
    }
}
Note: we must create new instance on every single iteration withing a loop, then only it could be, else single instance will get looped over the multiple times.
Output:
unique: 2750731359372300
unique: 2750731485912300
unique: 2750731491573500
unique: 2750731496781900
unique: 2750731501673200
unique: 2750731569698600
--------------------------------
--------------------------------
--------------------------------
*************************************************
public class GenerateUniqueRandNum {
    public static void main(String args[]) {
        long unqNum = System.nanoTime();
        for (int count = 0; count < 1000000; count++) {
            System.out.println("unique: " + unqNum* 100);
        }
    }

}
Output:
unique: 2946684697352300
unique: 2946684697352300
unique: 2946684697352300
unique: 2946684697352300
unique: 2946684697352300

unique: 2946684697352300
--------------------------------
--------------------------------
--------------------------------

No comments:

Post a Comment

How to run standalone mock server on local laptop

 Please download the standalone wiremock server from Direct download section at the bottom of the page.  Download and installation Feel fre...