Example usage for java.util UUID randomUUID

List of usage examples for java.util UUID randomUUID

Introduction

In this page you can find the example usage for java.util UUID randomUUID.

Prototype

public static UUID randomUUID() 

Source Link

Document

Static factory to retrieve a type 4 (pseudo randomly generated) UUID.

Usage

From source file:Main.java

/**
 * Generates a version 4 Universally Unique Identifier.
 * /*from w w  w .  j  a  v a  2  s. co m*/
 * @return
 */
public static String generateRandomUUID() {
    return UUID.randomUUID().toString();
}

From source file:Main.java

public static String getRandomNumber() {
    /*/*from  www .  j  a  va  2s . c  o  m*/
     * Random generator = new Random(); int r = generator.nextInt(1000);
     */
    return UUID.randomUUID().toString();

}

From source file:Main.java

/**
 * Generates 32 char long UUID without "-" char, in regex pattern "[0-9a-f]".
 *
 * @return//from   ww w.java  2  s .  co  m
 */
public static String generateUid() {
    return UUID.randomUUID().toString().replaceAll("-", "");
}

From source file:Main.java

/**
 * It generates a random UUID for the Android device.
 *
 * @return It returns the generated UUID.
 *///w  w w .  j  av  a 2 s . co  m
private static UUID generateUuid() {
    return UUID.randomUUID();
}

From source file:Main.java

/**
 * Retrieve a type 4 (pseudo randomly generated) UUID.
 *
 * @return A randomly generated {@code UUID}
 *//* w  ww  .  j ava  2 s  .c  o  m*/
public static String generateUUID() {
    return UUID.randomUUID().toString().replace("-", "").toLowerCase();
}

From source file:Main.java

public static long generateMpid() {
    while (true) {
        long id = hashFnv1A(UUID.randomUUID().toString().getBytes()).longValue();
        if (id != 0) {
            return id;
        }/*from   w  w  w .  ja va  2  s  .c  o m*/
    }
}

From source file:Main.java

public static byte[] randomUUID() {
    long n;/*from  ww w . ja va 2 s  .co  m*/
    byte[] result;
    UUID uuid;

    result = new byte[16];
    uuid = UUID.randomUUID();

    n = uuid.getMostSignificantBits();

    result[0] = (byte) (n >> 56 & 0xff);
    result[1] = (byte) (n >> 48 & 0xff);
    result[2] = (byte) (n >> 40 & 0xff);
    result[3] = (byte) (n >> 32 & 0xff);
    result[4] = (byte) (n >> 24 & 0xff);
    result[5] = (byte) (n >> 16 & 0xff);
    result[6] = (byte) (n >> 8 & 0xff);
    result[7] = (byte) (n & 0xff);

    n = uuid.getLeastSignificantBits();

    result[8] = (byte) (n >> 56 & 0xff);
    result[9] = (byte) (n >> 48 & 0xff);
    result[10] = (byte) (n >> 40 & 0xff);
    result[11] = (byte) (n >> 32 & 0xff);
    result[12] = (byte) (n >> 24 & 0xff);
    result[13] = (byte) (n >> 16 & 0xff);
    result[14] = (byte) (n >> 8 & 0xff);
    result[15] = (byte) (n & 0xff);

    return result;
}

From source file:Main.java

/**
 * Gets random string./*w  ww.j  a  v  a  2 s.  c  o  m*/
 *
 * @param length the length
 * @return the random string
 */
public static String getRandomString(int length) {
    String randomStr = UUID.randomUUID().toString();
    while (randomStr.length() < length) {
        randomStr = String.format("%s%s", randomStr, UUID.randomUUID().toString());
    }
    return randomStr.substring(0, length);
}

From source file:Main.java

public static String generateShortUuid() {
    UUID uuid = UUID.randomUUID();
    MessageDigest md = null;//from  w ww  .  ja v a 2s  .c  om
    try {
        md = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return uuid.toString();
    }

    md.update(uuid.toString().getBytes());
    byte[] digest = md.digest();

    return Base64.encodeToString(digest, Base64.URL_SAFE | Base64.NO_WRAP | Base64.NO_PADDING).substring(0, 20);
}

From source file:Main.java

public static String getOutputFilePath(File directory, String extension) throws IOException {
    ensureDirExists(directory);//  w ww  .ja  v  a2 s . c om
    String filename = UUID.randomUUID().toString();
    return directory + File.separator + filename + extension;
}