random String - Java java.lang

Java examples for java.lang:String Random

Description

random String

Demo Code


//package com.java2s;
import java.util.Random;

public class Main {
    public static void main(String[] argv) {
        int length = 42;
        System.out.println(randomString(length));
    }/*from  w ww  .  ja  va2s  .co  m*/

    private static Random randGen = null;
    private static char[] numbersAndLetters = null;
    private static Object initLock = new Object();

    public static final String randomString(int length) {

        if (length < 1) {
            return null;
        }
        if (randGen == null) {
            synchronized (initLock) {
                if (randGen == null) {
                    randGen = new Random();
                    numbersAndLetters = ("0123456789abcdefghijklmnopqrstuvwxyz"
                            + "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")
                            .toCharArray();
                    //numbersAndLetters = ("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
                }
            }
        }
        char[] randBuffer = new char[length];
        for (int i = 0; i < randBuffer.length; i++) {
            randBuffer[i] = numbersAndLetters[randGen.nextInt(71)];
            //randBuffer[i] = numbersAndLetters[randGen.nextInt(35)];
        }
        return new String(randBuffer);
    }
}

Related Tutorials