get Random String - Java java.lang

Java examples for java.lang:String Random

Description

get Random String

Demo Code


//package com.java2s;

import java.util.Random;

public class Main {
    private static final char[] SYMBOLS = "abcdefghijklmnopqrstuvwxyz0123456789"
            .toCharArray();// w  w  w.j a v  a 2  s.c o  m

    public static String getRandomString(int length) {
        StringBuilder stringBuilder = new StringBuilder();
        Random random = new Random();

        for (int i = 0; i < length; i++) {
            char c = SYMBOLS[random.nextInt(SYMBOLS.length)];
            stringBuilder.append(c);
        }

        return stringBuilder.toString();
    }
}

Related Tutorials