Java ThreadLocalRandom randomString(int minLength, int maxLength)

Here you can find the source of randomString(int minLength, int maxLength)

Description

random String

License

Apache License

Declaration

public static String randomString(int minLength, int maxLength) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

public class Main {
    public static String randomString(int minLength, int maxLength) {
        Random random = ThreadLocalRandom.current();
        return randomString(minLength, maxLength, random);
    }/*  w w  w.  j a v  a 2s. c  o  m*/

    public static String randomString(int minLength, int maxLength, Random random) {
        return randomStringBuilder(minLength, maxLength, random).toString();
    }

    public static StringBuilder randomStringBuilder(int minLength, int maxLength, Random random) {
        int length;
        if (minLength > maxLength)
            throw new IllegalArgumentException();
        if (minLength == maxLength) {
            length = minLength;
        } else {
            length = minLength + random.nextInt(maxLength - minLength);
        }
        StringBuilder sb = new StringBuilder(length);
        for (int i = length; i > 0; --i) {
            sb.append((char) (random.nextInt('Z' - 'A') + 'A'));
        }
        return sb;
    }
}

Related

  1. randomPer(long num, long divisor)
  2. randomReal(float range)
  3. randomSeed()
  4. randomString()
  5. RandomString(int length)
  6. randomString(List strings)
  7. randomStringArray(int arrayLength, int stringLength)
  8. randomWorld()
  9. randomWorld()