Java Random String getRandomString(String paramString, int paramLength)

Here you can find the source of getRandomString(String paramString, int paramLength)

Description

Generate Random String

License

Apache License

Parameter

Parameter Description
paramString String from the Random string created
paramLength length of random string created

Return

random string containing character from above paramString of length paramLength

Declaration

public static String getRandomString(String paramString, int paramLength) 

Method Source Code

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

import java.util.Random;

public class Main {
    /**// w w  w.  ja v a2s  .co  m
     * Generate Random String
     * @param  paramString String from the Random string created
     * @param paramLength length of random string created
     * @return random string containing character from above paramString of length paramLength
     */
    public static String getRandomString(String paramString, int paramLength) {
        if (paramLength < 1) {
            throw new IllegalArgumentException("paramLength must be positive");
        }
        StringBuilder stringBuilder = new StringBuilder(paramLength);
        Random random = new Random();
        for (int j = 0; j < paramLength; j++) {
            stringBuilder.append(paramString.charAt(random.nextInt(paramString.length())));
        }
        return stringBuilder.toString();
    }
}

Related

  1. getRandomString(int size)
  2. getRandomString(int strLength)
  3. getRandomString(Random random, int minLen, int maxLen)
  4. getRandomString(Random random, int totalLength, int randomSectionLength)
  5. getRandomString(Random rnd, int minLength, int maxLength)
  6. getRandomStringByLength(int length)
  7. getRandomStringForDate()
  8. getRandomStringOfLetters(int length)
  9. getString(int length)