Java: Random

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
 * encoding: utf-8
 * 版权所有 2023 涂聚文有限公司
 * 许可信息查看:
 * 描述:        //https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/RandomStringUtils.html
 *         //https://commons.apache.org/proper/commons-lang/download_lang.cgi
 * # Author    : geovindu,Geovin Du 涂聚文.
 * # IDE       : IntelliJ IDEA 2023.1 Java 17
 * # Datetime  : 2023 - 2023/12/16 - 6:20
 * # User      : geovindu
 * # Product   : IntelliJ IDEA
 * # Project   : javademo
 * # File      : ApacherandomHelper.java  类
 * # explain   : 学习
 **/
 
package Common;
 
 
import org.apache.commons.lang3.*; //3.14.0
import java.util.Random;
import java.lang.String;
 
/**
 * 随机数或随机字符串操作
 */
public class ApacherandomHelper {
 
 
    /**
     * 随机数
     * @param length 长度
     * @return 返回随机数字串
     */
    public  static String getnumber(int length)
    {
 
        // Creates a 8 chars length random string of number.
        String result = RandomStringUtils.random(length, false, true);
        //System.out.println("random = " + result);
        return  result;
    }
 
    /**
     *随机字符串
     * @param length 长度
     * @return
     */
    public static String getAphabetic(int length)
    {
        String result="";
        // Creates a 8 chars length of random alphabetic string.
        result = RandomStringUtils.randomAlphabetic(length);
        //System.out.println("random = " + result);
        return  result;
 
    }
 
    /**
     *随机Ascii 字符,有特殊字符,数字,字母等
     * @param length 长度
     * @return
     */
    public  static String getAscii(int length)
    {
        String result="";
        // Creates a 8 chars length of random ascii string.
        result = RandomStringUtils.randomAscii(length);
        //System.out.println("random = " + result);
        return result;
 
    }
 
    /**
     * 自定义长度和自定义随朵的字符串 可以和数字字母特殊字符
     * @param length 长度
     * @param word 自定义的自符串  23456789abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ
     * @return 返回随机的字符串
     */
    public  static  String CustomerCharacters(int length,String word)
    {
        String result="";
        // Creates a 6 chars length of string from the defined array of
        // characters including numeric and alphabetic characters.
        //String word="23456789abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ";
        int wordlength=word.length();
        result = RandomStringUtils.random(6, 0, wordlength, true, true,word.toCharArray());
        //System.out.println("random = " + result);
        return  result;
    }
 
}

  

调用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import Common.ApacherandomHelper;
 
public class Main {
 
    /**
     *
     * @param args
     */
    public static void main(String[] args)
    {
 
 
        System.out.println("Hello java language world! 涂聚文!");
 
        //随机字符串
        System.out.println(ApacherandomHelper.getnumber(6));
        System.out.println(ApacherandomHelper.getAscii(6));
        System.out.println(ApacherandomHelper.getAphabetic(6));
        System.out.println(ApacherandomHelper.CustomerCharacters(6,"23456789abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ"));
 
}

  

输出:

 

posted @   ®Geovin Du Dream Park™  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示