Java Random String getRandomStr(int length)

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

Description

get Random Str

License

Apache License

Declaration

public static String getRandomStr(int length) 

Method Source Code

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

import java.util.Random;

public class Main {

    public static String getRandomStr(int length) {
        Random randGen = null;//from  w  w  w.j av a2s .  co  m
        char[] numbersAndLetters = null;
        Object initLock = new Object();
        if (length < 1) {
            return null;
        }
        if (randGen == null) {
            synchronized (initLock) {
                if (randGen == null) {
                    randGen = new Random();
                    numbersAndLetters = ("0123456789abcdefghijklmnopqrstuvwxyz"
                            + "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
                }
            }
        }
        char[] randBuffer = new char[length];
        for (int i = 0; i < randBuffer.length; i++) {
            randBuffer[i] = numbersAndLetters[randGen.nextInt(71)];
        }
        return new String(randBuffer);
    }
}

Related

  1. getRandomStr()
  2. getRandomStr()
  3. getrandomstr()
  4. getRandomStr()
  5. getRandomStr(int Len)
  6. GetRandomStr(int length)
  7. getRandomStr(int length)
  8. getRandomStr(int n)
  9. getRandomStr(int qty)