create Mix Upper Case and Lower Case Random String - Java java.lang

Java examples for java.lang:String Random

Description

create Mix Upper Case and Lower Case Random String

Demo Code


//package com.java2s;

public class Main {
    public static final String ALL_NUMBER_VALUE = "0123456789";
    public static final String ALL_LOWERCASE_VALUE = "abcdefghijklmnopqrstuvwxyz";
    public static final String ALL_UPPERCASE_VALUE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    public static String createMixRandom(int length) {
        if (length < 1) {
            throw new IllegalArgumentException(
                    "length cannot be less than 1");
        }/* w w  w.j a v a  2  s.  co  m*/
        String values = ALL_NUMBER_VALUE + ALL_UPPERCASE_VALUE
                + ALL_LOWERCASE_VALUE;
        int vLen = values.length();
        String result = "";
        for (int i = 0; i < length; i++) {
            double dr = Math.random() * vLen;
            int ir = (int) Math.floor(dr);
            result += values.charAt(ir);
        }
        return result;
    }
}

Related Tutorials