日期:2014-05-20  浏览次数:20688 次

想用下面的代码随机产生两个不同的单字符字符串,如“A”,“8”,请帮看看,什么实际仍然会重复?
Java code

str1=GetRandomCharString("");;
str3=GetRandomCharString(str1);;


   private String GetRandomCharString(String strExclude){
        Random randGen = null;
        char[] numbersAndLetters = null;

        if (randGen == null) 
        {
            randGen = new Random();
            numbersAndLetters = ("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
        }
        char randC;
        String randStr="";
        randC = numbersAndLetters[randGen.nextInt(61)];
        randStr=String.valueOf(randC);
        while(strExclude.indexOf(randStr)>0){            
            randC = numbersAndLetters[randGen.nextInt(61)];
            randStr=String.valueOf(randC);
        }
        return randStr;
    }


------解决方案--------------------
不稳定算法,不知道什么时候还会重复