美文网首页
产生10万个长度不超过10的字符串,包含a-z,A-Z

产生10万个长度不超过10的字符串,包含a-z,A-Z

作者: xiaohei_e853 | 来源:发表于2022-03-11 11:40 被阅读0次
/**
 * 产生10万个长度不超过10的字符串,包含a-z,A-Z
 */
public class GenerateString {
    public static void main(String[] args) throws IOException {
        FileWriter fw =  new FileWriter("words.txt");

        for (int i = 0; i < 100000; i++) {
            //1 - 10
           int length = (int)(Math.random() * (10 - 1 + 1) + 1);
            fw.write(getString(length) + "\n");
        }

        fw.close();
    }

    public static String getString(int length){
        String str = "";
        for (int i = 0; i < length; i++) {
            //65 - 90, 97-122
            int num = (int)(Math.random() * (90 - 65 + 1) + 65) + (int)(Math.random() * 2) * 32;
            str += (char)num;
        }
        return str;
    }
}

相关文章

网友评论

      本文标题:产生10万个长度不超过10的字符串,包含a-z,A-Z

      本文链接:https://www.haomeiwen.com/subject/kgmcdrtx.html