Java: Random

/**
 * 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;
    }
 
}

调用:

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"));
 
}

输出:

Java: Random_第1张图片

你可能感兴趣的:(Java,java,开发语言)