Javaは、固定長のランダム文字列(大文字と小文字と数字)を生成します.

3877 ワード

package org.jimmy.autosearch2019.test;

import java.util.ArrayList;
import java.util.Random;

/**
 * @author ラピスラズリ(Dawn)
 * @date 2019 5 30    3:18:48
 * @detail             
 */
public class TestRandom2019053001 {

    public static ArrayList strList = new ArrayList();
    public static Random random = new Random();
    public static final int RANDOM_LENGTH1 = 256;
    public static final int RANDOM_LENGTH2 = 512;
    public static final int RANDOM_LENGTH3 = 1024;
    
    static {
        init();
    }
    
    public static void main(String[] args) {
        String randomStr = generateRandomStr(RANDOM_LENGTH1);
        System.out.println(RANDOM_LENGTH1 + "    :" + randomStr);
        randomStr = generateRandomStr(RANDOM_LENGTH2);
        System.out.println(RANDOM_LENGTH2 + "    :" + randomStr);
        randomStr = generateRandomStr(RANDOM_LENGTH3);
        System.out.println(RANDOM_LENGTH3 + "    :" + randomStr);
    }
    
    public static String generateRandomStr(int length) {
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < length; i++) {
            int size = strList.size();
            String randomStr = strList.get(random.nextInt(size));
            sb.append(randomStr);
        }
        return sb.toString();
    }
    
    public static void init() {
        int begin = 97;
        //      ,     
        for(int i = begin; i < begin + 26; i++) {
            strList.add((char)i + "");
        }
        //      ,     
        begin = 65;
        for(int i = begin; i < begin + 26; i++) {
            strList.add((char)i + "");
        }
        // 0-9       
        for(int i = 0; i < 10; i++) {
            strList.add(i + "");
        }
    }
    
}

実行後の効果:
256ビット乱数:qZP 66 hBKD 2 qZUNNw 0 seH 2 auRjw 7 wvtVVVVAUalV 6 ZZZZZZXzL 8 H 6 MXSKcA 3 a 5 ae116 DhMLD 0 jXlbNQp 1 YlRpDIpijbn 9 EtqPlWsIbvlrE 1 fZtnY 8 K 88 N 6 uhOYsCZc 44 A 064 arq 0 bK 4 tkCvVcXLPTyGeHJkBfujOrQfpkkyRGGGVVVVVVVVVVVVkkkkkkkkkpkkkydRGVVVVVVVVVVVVVVtyDUjBpNf 393 WRygZKSDUy 704 B 2 h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