Site icon

Generate/create random GUID or UUID in java (JUG library/example)

1. Maven dependency of JUG library


<dependency>
	<groupId>com.fasterxml.uuid</groupId>
	<artifactId>java-uuid-generator</artifactId>
	<version>3.1.3</version>
</dependency>

2. Generate or create random GUID/UUID in java (Example/ JUG library)

package org.learn.uuid;

import com.fasterxml.uuid.Generators;
import com.fasterxml.uuid.NoArgGenerator;

import java.util.Random;
import java.util.UUID;

public class RandomUUID {
    public static void main(String[] args) {

        NoArgGenerator randomBasedGenerator = Generators.randomBasedGenerator();

        //Generate random UUID generator
        UUID firstUUID = randomBasedGenerator.generate();
        System.out.printf("1. First UUID is : %s ", firstUUID.toString());

        UUID secondUUID = randomBasedGenerator.generate();
        System.out.printf("\n2. Second UUID is :%s ", secondUUID.toString());

        //Generate custom UUID from java.util.random
        randomBasedGenerator = Generators.randomBasedGenerator(new Random(100));
        UUID customUUID = randomBasedGenerator.generate();
        UUID anotherCustomUUID = randomBasedGenerator.generate();

        System.out.printf("\n3. Custom UUID using random number :%s ", customUUID.toString());
        System.out.printf("\n4. Another custom UUID : %s ", anotherCustomUUID.toString());
    }
}

3. Output: Generate random GUID/UUID in java (Example/ JUG library)

1. First UUID is : 0dcc3525-21df-4d9b-8c35-2521dfbd9bac 
2. Second UUID is :78954fe7-d882-4e62-954f-e7d8824e6223 
3. Custom UUID using random number :b8d59fd5-bc12-4bb4-b1e9-f344b73ee369 
4. Another custom UUID : aacaf867-070c-4774-8747-e6984d95fa51 
Exit mobile version