| random是用来生成随机数的,简单来说也可以得随机的几率,但这个跟概率没有关系。。。两个概念 下边这个根据几率做一些功能的例子
 复制代码import java.util.Random;
public class ran
{
        static Random random = new Random();
        public static void main(String[] args)
        {
                // 第一个参数100表示比例,如果要求千分比就是1000
                // 第二个参数是表示要拿几个随机的比例
                int[] randomArray = getRandomNums(100, 10);
                // 假设我有一个功能,有35的几率说你好,有45的几率说hello,另外20%的几率说对不起
                for (int i : randomArray)
                {
                        if(i < 35)
                        {
                                System.out.println("你好");
                        }
                        else if(i < 80){
                                System.out.println("hello");
                        }
                        else {
                                System.out.println("对不起");
                        }
                }
                
                
        }
        
        public static int[] getRandomNums(int scale, int nums)
        {
                int[] temp = new int[nums];
                
                for(int i = 0; i < nums; i++)
                {
                        temp[i] = getRandom(scale);
                }
                
                return temp;
        }
        
        public static int getRandom(int scale)
        {
                return Math.abs(random.nextInt() % scale);
        }
}
 |