如何生成随机字符串并将其放入HashTable?

我想生成一个随机的唯一字符串并放入哈希表。我确实喜欢下面的代码,但它不是唯一的,它只是一个字符。


while(k<4){

    for(int j=1 ; j<=13 ;j++){

        Hashtable<Integer, String> Deck = new Hashtable();  

        int myChar = (int) (Math.random()*str.length());

        StringBuilder sb = new StringBuilder();

        sb.append(str.charAt(myChar)); 


        int i = (int) (Math.random() *52) ; 

        Deck.put(i, sb.toString());

        System.out.print(Deck);

    }


    System.out.println();

    k++;

}

输出是这个


{30=d}{9=e}{11=b}{10=d}{43=g}....

我想成为


{30=dea}{9=egt}{11=brf}{10=dgrtg}{43=grrh}{14=gwrt}.....


阿晨1998
浏览 97回答 3
3回答

达令说

您可以使用 Apache Commons Lang 生成字母字符串String generatedString = RandomStringUtils.randomAlphanumeric(10);尝试这样的事情&nbsp; &nbsp; Hashtable<Integer, String> Deck = new Hashtable();&nbsp; &nbsp; for (int i = 0; i < 10; i++) {&nbsp; &nbsp; &nbsp; &nbsp; String generatedString = RandomStringUtils.randomAlphanumeric(10);&nbsp; &nbsp; &nbsp; &nbsp; int key = (int) (Math.random() *52) ;&nbsp; &nbsp; &nbsp; &nbsp; Deck.put(key, generatedString);&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println(Deck);输出{9=Ut7N87oMNp, 8=7kbARh5WIy, 7=pbU2ZCOGK1, 6=vAGAIw41Us, 5=VLnpY1FAuN, 4=UEIJNIvZlt, 3=z6Y3zXcDY1, 2=PxaMqXl8XW, 1=l72bkPdY6T, 0=FFdOsKpQgd}如果你只想要 Alphabets 然后使用RandomStringUtils.randomAlphabetic(10);instaed of RandomStringUtils.randomAlphanumeric(10)。如果您不想要第三方 api,那么您的代码片段会进行一些更改while (k < 4) {&nbsp; &nbsp; &nbsp; &nbsp; for (int j = 1; j <= 13; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Hashtable<Integer, String> Deck = new Hashtable();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StringBuilder sb = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int cnt = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (cnt++ != 10) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int myChar = (int) (Math.random() * str.length());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.append(str.charAt(myChar));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int i = (int) (Math.random() * 52);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Deck.put(i, sb.toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(Deck);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; k++;&nbsp; &nbsp; }仅添加了while循环...

四季花海

您可以使用以下代码生成随机字符串。private static final String ALPHA_STRING = "abcdefghijklmnopqrstuvwxyz";public static String randomString(int count) {&nbsp; &nbsp; StringBuilder builder = new StringBuilder();&nbsp; &nbsp; while (count-- != 0) {&nbsp; &nbsp; &nbsp; &nbsp; int character = (int) (Math.random() * ALPHA_STRING.length());&nbsp; &nbsp; &nbsp; &nbsp; builder.append(ALPHA_STRING.charAt(character));&nbsp; &nbsp; }&nbsp; &nbsp; return builder.toString();}public static void main(String[] arg) {&nbsp; &nbsp; Hashtable<Integer, String> Deck = new Hashtable();&nbsp; &nbsp; for (int j = 1; j <= 10; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Deck.put(j, randomString(12));&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println(Deck);}输出{10=upjolkfihcyh, 9=irpbrcoclcmx, 8=ppugclscjlja, 7=wtnvkpmocuwd, 6=gpyzppvywazp, 5=vosixtjvcqlg, 4=plwtzzjpzeoq, 3=wsdyyppphhng, 2=knqdzfctfnez, 1=jzakqfiksrho}

慕尼黑5688855

尝试使用以下代码使用随机方法生成唯一密钥。private char[] uniqueKeyGenerator(int len) {&nbsp; &nbsp; &nbsp; &nbsp; String Capital_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";&nbsp; &nbsp; &nbsp; &nbsp; String Small_chars = "abcdefghijklmnopqrstuvwxyz";&nbsp; &nbsp; &nbsp; &nbsp; String numbers = "0123456789";&nbsp; &nbsp; &nbsp; &nbsp; String values = Capital_chars + Small_chars + numbers;&nbsp; &nbsp; &nbsp; &nbsp; // Using random method&nbsp; &nbsp; &nbsp; &nbsp; Random rndm_method = new Random();&nbsp; &nbsp; &nbsp; &nbsp; char[] password = new char[len];&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < len; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; password[i] = values.charAt(rndm_method.nextInt(values.length()));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return password;&nbsp; &nbsp; }&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java