get Random String - Java java.util

Java examples for java.util:Random String

Description

get Random String

Demo Code


//package com.java2s;

public class Main {
    static final String charFactory = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

    public static String getRandomString(int length) {
        StringBuilder str = new StringBuilder();
        int tempLength = charFactory.length();
        for (int i = 0; i < length; i++) {
            double tempDouble = Math.random() * tempLength;
            int tempInt = (int) Math.floor(tempDouble);
            str.append(charFactory.charAt(tempInt));
        }//from  w w w .  j  a  v  a 2s .co  m

        return str.toString();
    }
}

Related Tutorials