Java Random String generateRandomString(String str, int length)

Here you can find the source of generateRandomString(String str, int length)

Description

generate Random String

License

Apache License

Declaration

public static String generateRandomString(String str, int length) 

Method Source Code

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

import java.util.Random;

public class Main {
    private static final Random _RAND = new Random(System.currentTimeMillis());
    private static final String _CHARTECER = "ABCDEFGHIJKLMNOPUVWXYZ0123456789";

    public static String generateRandomString(String str, int length) {
        String encoder = null;/*from w  ww .  j  a  va  2 s .c o m*/
        if (str == null || str.trim().length() == 0) {
            encoder = _CHARTECER;
        } else {
            encoder = str.trim();
        }
        StringBuilder randomString = new StringBuilder();
        int limit = encoder.length();
        for (int index = 0; index < length; index++) {
            randomString.append(encoder.charAt(_RAND.nextInt(limit)));
        }
        return randomString.toString();
    }
}

Related

  1. generateRandomString(int stringLength)
  2. generateRandomString(int wordsLength, String separator)
  3. generateRandomString(Random random, int length)
  4. generateRandomString(Random random, int size)
  5. generateRandomString(Random rnd, char[] alphabet, int maxLength)
  6. genRandomString()
  7. genRandomString(final int length)
  8. genRandomString(int count)
  9. getDefaultRandomName(String namePrefix)