Java Random String getRandomString(int length)

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

Description

get Random String

License

Open Source License

Declaration

public static String getRandomString(int length) 

Method Source Code


//package com.java2s;

import java.util.Random;

public class Main {
    private static char[] baseData = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
            'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
            'Z' };
    private static int baseDataLength = baseData.length;

    public static String getRandomString(int length) {
        char[] data = new char[length];
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            data[i] = baseData[random.nextInt(baseDataLength)];
            //data[i] = baseData[i];
        }/* w  ww.j a v  a 2s .c o m*/
        return new String(data);
    }
}

Related

  1. getRandomString(int length)
  2. getRandomString(int length)
  3. getRandomString(int length)
  4. getRandomString(int length)
  5. getRandomString(int length)
  6. getRandomString(int length)
  7. getRandomString(int length)
  8. getRandomString(int length)
  9. getRandomString(int length)