get Random Keys - Java java.lang

Java examples for java.lang:String Random

Description

get Random Keys

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        int intLength = 42;
        System.out.println(getRandKeys(intLength));
    }/*from  w ww. j  a v  a2 s . c o m*/

    public static String getRandKeys(int intLength) {

        String retStr = "";
        String strTable = "23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz@=";
        int len = strTable.length();
        boolean bDone = true;
        do {
            retStr = "";
            int count = 0;
            for (int i = 0; i < intLength; i++) {
                double dblR = Math.random() * len;
                int intR = (int) Math.floor(dblR);
                char c = strTable.charAt(intR);
                if (('0' <= c) && (c <= '9')) {
                    count++;
                }
                retStr += strTable.charAt(intR);
            }
            if (count >= 2) {
                bDone = false;
            }
        } while (bDone);

        return retStr;
    }
}

Related Tutorials