Java UUID.randomUUID() method example

In this tutorial, You will learn how to generate random UUID number. Java UUID.randomUUID() method returns a randomly generated UUID.

Static factory to retrieve a type 4 (pseudo randomly generated) UUID. The UUID is generated using a cryptographically strong pseudo random number generator.

syntax

public static UUID randomUUID()

Returns

A randomly generated UUID.

Java UUID randomUUID() example

  • Create random UUID by invoking static method UUID.randomUUID();
  • Print the randomly generated UUID's.
package com.technicalkeeda;

import java.util.UUID;

public class App {

    public static void main(String[] args) {

        UUID randomUUID = UUID.randomUUID();

        System.out.println("Random UUID :-  " + randomUUID);
        System.out.println("Random UUID :-  " + UUID.randomUUID());
        System.out.println("Random UUID :-  " + UUID.randomUUID());
        System.out.println("Random UUID :-  " + UUID.randomUUID());

    }
}

output

Random UUID :-  860c5c50-274f-4cb9-9735-c7cc58b9d701
Random UUID :-  d7660de1-da71-48a4-81db-494a002b5b5e
Random UUID :-  6579cf0a-8a51-4b4e-bda6-f4b9764c2d01
Random UUID :-  d33c697d-677d-4178-8e86-60880edabd05