Java: Random

发布时间 2023-04-14 12:12:58作者: ®Geovin Du Dream Park™

 

/**
 * 版权所有 2023 涂聚文有限公司
 * 许可信息查看:
 * 描述:
 *用100元买100只鸡,大公鸡5元一只,母鸡3元1只,小鸡一元3只,问各能买多少只?
 * 历史版本: JDK 8.01
 * 2023-03-12 创建者 geovindu
 * 2023-03-12 添加 Lambda
 * 2023-03-12 修改:date
 * 接口类
 * 2023-03-12 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc RandomHelper.java
 * Interface
 * Record
 * Annotation
 * Enum
 * */

package Common;

import java.util.Random;


/**
 *
 * @author geovindu 凃聚文,塗聚文,Geovin Du
 * @version  1.0
 * 
 * 隨機數生成
 *
 *
 * */
public class RandomHelper {

    /**
     *
     * */
   static Random random=new Random();

    static String randomChars = "ABCDFGHJKMNPQRTVWXYabcdefghjkmnpqrtvwxy23456789!#@%$&";
    /**
     *
     * @param max 随大值
     * @param min 最小值
     * @return
     * */
    public static int RandomInt(int min, int max)
    {

        int num=random.nextInt(max-min+1)+min;
        return num;
    }
    /**
     *指定字符串自定義長度
     * @param lengh 自定長度
     * @return 返回需要的字符串
     * */
    public static String RandomString(int lengh)
    {
        String getCode="";
        StringBuilder stringBuilder=new StringBuilder();
        for (int i = 0; i <lengh ; i++) {
            int num=random.nextInt(randomChars.length());
           stringBuilder.append(randomChars.charAt(num));
        }
        getCode=stringBuilder.toString();
        return  getCode;
    }
    /**
     *
     * @param lengh 自定義抽取几個
     *
     * */
    public static String RandomArryString(int lengh)
    {
        String getCode="";
        String [] arryStr=new String[] {"Broccoli", "SiboDu", "Kiwi", "Kale", "Toma","GeovinDu","塗聚文"};
        StringBuilder stringBuilder=new StringBuilder();
        for (int i = 0; i <lengh ; i++) {
            int num=random.nextInt(arryStr.length);
            stringBuilder.append(arryStr[num]);
        }
        getCode=stringBuilder.toString();
        return  getCode;
    }
    /**
     * 自定義字符串和長度
     * @param lengh 自定長度
     * @param strMore 自定字符串
     * @return 返回需要的字符串
     *
     * */
    public static String RandomString(int lengh,String strMore)
    {
        String getCode="";
        StringBuilder stringBuilder=new StringBuilder();
        for (int i = 0; i <lengh ; i++) {
            int num=random.nextInt(strMore.length());
            stringBuilder.append(strMore.charAt(num));
        }
        getCode=stringBuilder.toString();
        return  getCode;
    }
    /**
     *
     * @param lengh 自定義抽取几個
     * @param arryStr
     * @return
     * */
    public static String RandomArryString(int lengh,String [] arryStr)
    {
        String getCode="";
        //String [] arryStr=new String[] {"Broccoli", "SiboDu", "Kiwi", "Kale", "Toma","GeovinDu","塗聚文"};
        StringBuilder stringBuilder=new StringBuilder();
        for (int i = 0; i <lengh ; i++) {
            int num=random.nextInt(arryStr.length);
            stringBuilder.append(arryStr[num]);
        }
        getCode=stringBuilder.toString();
        return  getCode;
    }
}


/**
 * 版权所有 2023 涂聚文有限公司
 * 许可信息查看:
 * 描述:
 *用100元买100只鸡,大公鸡5元一只,母鸡3元1只,小鸡一元3只,问各能买多少只?
 * 历史版本: JDK 8.01
 * 2023-03-12 创建者 geovindu
 * 2023-03-12 添加 Lambda
 * 2023-03-12 修改:date
 * 接口类
 * 2023-03-12 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc RandomDal.java
 * Interface
 * Record
 * Annotation
 * Enum
 * */

package Dal;


import Common.RandomHelper;

/**
 *
 *
 *
 * */
public class RandomDal {


    /**
     *
     * @return
     * */
    public String getRandomString()
    {
        //生成字符串位數隨機
        int lengh=RandomHelper.RandomInt(6,10); //六位至十位之閒
        return  RandomHelper.RandomString(lengh);

    }
    /**
     *
     * @param lengh
     * @return
     * */
    public String getRandomString(int lengh)
    {

        return  RandomHelper.RandomString(lengh);

    }
    /**
     *
     * @return
     * */
    public int getRandomInt()
    {

        return RandomHelper.RandomInt(0,10);
    }
    /**
     *
     * @param min
     * @param max
     * @return
     * */
    public int getRandomInt(int min,int max)
    {

        return RandomHelper.RandomInt(min,max);
    }
    /**
     *
     * @return
     * */
    public String getRandomArryStr()
    {
        return RandomHelper.RandomArryString(1);
    }
    /**
     *
     * @param count
     * @return
     *
     * */
    public String getRandomArryStr(int count)
    {
        return RandomHelper.RandomArryString(count);
    }
    /**
     *
     * @param count
     * @param arry
     * @return
     *
     * */
    public String getRandomArryStr(int count,String [] arry)
    {
        return RandomHelper.RandomArryString(count,arry);
    }
}

/**
 * 版权所有 2023 涂聚文有限公司
 * 许可信息查看:
 * 描述:
 *用100元买100只鸡,大公鸡5元一只,母鸡3元1只,小鸡一元3只,问各能买多少只?
 * 历史版本: JDK 8.01
 * 2023-03-12 创建者 geovindu
 * 2023-03-12 添加 Lambda
 * 2023-03-12 修改:date
 * 接口类
 * 2023-03-12 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc RandomBll.java
 * Interface
 * Record
 * Annotation
 * Enum
 * */

package Bll;


import  Dal.RandomDal;

/**
 *
 *
 *
 * */
public class RandomBll {


    /**
     *
     *
     * */
    RandomDal randomDal=new RandomDal();

    /**
     *
     * @return
     * */
    public String getRandomString() {

        String str=randomDal.getRandomString();
        return  str;
    }
    /**
     *
     * @return ;
     * */
    public int getRandomInt()
    {
        int num=randomDal.getRandomInt();
        return  num;
    }
    /**
     *
     * @param min
     * @param max
     * @return
     * */
    public int getRandomInt(int min,int max)
    {
        int num=randomDal.getRandomInt(min,max);
        return  num;
    }
    /**
     *
     *
     *
     * */
    public String getRandomArryStr()
    {
        return  randomDal.getRandomArryStr();
    }
}

  

调用:

  //隨機數
        RandomBll randomBll=new RandomBll();
        for (int i = 0; i <10 ; i++) {
            System.out.println("int:"+randomBll.getRandomInt());
            System.out.println("int(6-10):"+randomBll.getRandomInt(6,10));
            System.out.println("string:"+randomBll.getRandomString());
            System.out.println("抽獎:"+randomBll.getRandomArryStr());
        }

  

输出:

int:9
int(6-10):9
string:j5Ntq!c
抽獎:SiboDu
int:0
int(6-10):6
string:hD3Wg4FVBQ
抽獎:Broccoli
int:8
int(6-10):6
string:m78378w9c
抽獎:Broccoli
int:7
int(6-10):8
string:RRQKTk6T
抽獎:GeovinDu
int:5
int(6-10):7
string:tR5Q6qdBvj
抽獎:Kiwi
int:10
int(6-10):10
string:t8KtNer
抽獎:Kiwi
int:3
int(6-10):10
string:tWA8Tgqc
抽獎:Broccoli
int:5
int(6-10):10
string:g4q8e#xgyN
抽獎:Toma
int:10
int(6-10):6
string:!NhddKb!
抽獎:Toma
int:1
int(6-10):7
string:c8WQvN97NX
抽獎:Toma
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99]
1--100以内的奇数的和是:2500
[5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100]
1--100以内除五的数的和是:1050
输入10个整数(按回车输入另一个数):