Java Random String getRandomString(final int length)

Here you can find the source of getRandomString(final int length)

Description

returns a random string of characters (A-Z) of the specified length

License

Open Source License

Parameter

Parameter Description
length the length of the string

Return

random string

Declaration


public static String getRandomString(final 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 a  va  2s . c  om*/
     *  returns a random string of characters (A-Z) of the specified length
     * 
     *  @param length the length of the string
     *  @return random string
     * 
     */

    public static String getRandomString(final int length) {

        final StringBuffer sb = new StringBuffer();
        final Random rand = new Random();

        for (int i = 0; i < length; i++)
            sb.append((char) ((Math.abs(rand.nextInt()) % 26) + 65));

        return sb.toString();

    }
}

Related

  1. getRandomString()
  2. getRandomString()
  3. getRandomString()
  4. getRandomString(final int len)
  5. getRandomString(final int len)
  6. getRandomString(final int size)
  7. getRandomString(final int size)
  8. getRandomString(int cantidad, boolean mayusculas, boolean minusculas, boolean numeros, boolean simbolos, boolean repetir)
  9. getRandomString(int count)