Java ThreadLocalRandom getRandomId(@Nonnull Random random, int length)

Here you can find the source of getRandomId(@Nonnull Random random, int length)

Description

get Random Id

License

Open Source License

Declaration

@Nonnull
    public static String getRandomId(@Nonnull Random random, int length) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import javax.annotation.Nonnull;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

public class Main {
    private static final int RANDOM_ID_LENGTH = 14;
    private static final char[] RANDOM_ID_CHARS = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
            'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
            'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2',
            '3', '4', '5', '6', '7', '8', '9', '+', '/' };

    @Nonnull
    public static String getRandomId(@Nonnull Random random, int length) {
        final char[] buf = new char[length];
        for (int i = 0; i < length; ++i) {
            buf[i] = RANDOM_ID_CHARS[random.nextInt(RANDOM_ID_CHARS.length)];
        }//from   w  w w.  ja va2 s .  c  om
        return new String(buf);
    }

    @Nonnull
    public static String getRandomId() {
        return getRandomId(ThreadLocalRandom.current(), RANDOM_ID_LENGTH);
    }
}

Related

  1. getRandomBoundedInt(int bound)
  2. getRandomDouble(double a, double b)
  3. getRandomElement(E[] array)
  4. getRandomElement(List elements)
  5. getRandomFloat()
  6. getRandomInt()
  7. getRandomInt(int a, int b)
  8. getRandomInt(int limit)
  9. getRandomInt(int n)