Java Random String generateRandomStr(int length)

Here you can find the source of generateRandomStr(int length)

Description

Generate a random string.

License

Open Source License

Parameter

Parameter Description
length Length of string

Return

Random string of given length

Declaration

public static String generateRandomStr(int length) 

Method Source Code


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

import java.util.Random;

public class Main {
    /**//from   w  w w. j  ava  2 s .  co  m
     * Generate a random string.
     * @param length Length of string
     * @return Random string of given length
     */
    public static String generateRandomStr(int length) {
        String candidateChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            sb.append(candidateChars.charAt(random.nextInt(candidateChars.length())));
        }

        return sb.toString();
    }
}

Related

  1. generateRandomChain(int length)
  2. generateRandomCharacters(int N)
  3. generateRandomCode()
  4. generateRandomSentence()
  5. generateRandomStr(final int length)
  6. generateRandomString()
  7. generateRandomString()
  8. generateRandomString()
  9. generateRandomString()