生成随机数组

生成随机数算法

 1 package com.util;
 2 
 3 import java.util.Random;
 4 public class Utils {
 5 
 6     /**
 7      * 工具类20190604
 8      */
 9     
10     /**
11      * 生成int类型随机的数组
12      * @param min  最小值
13      * @param max  最大值
14      * @param count 个数
15      * @return 
16      */
17     
18     public static int[] getRandomIntArray(int min,int max,int count) {
19         //得到一个最大的数组(随机数的范围)
20         int len = max - min + 1;
21          if (max < min || count > len) {
22                 return null;
23             }
24         
25         //生成一个空的结果随机数组
26         int [] result  = new int[count];
27         //得到随机数数组
28         int [] source =  new int[len];
29         //给随机数数组倒序赋值
30         /* 对循环进行改进  */
31 //        for(int i=min,j=0;j<len;i++,j++) {
32 //            source[len -j -1 ] = i;
33 //        }
34         for(int i=min;i<=max;i++) {
35             source[i-min] = i;
36         }
37         
38         //生成随机数
39         Random r = new Random();
40         int index = 0;
41         int random;
42         for(int i = 0;i<result.length;i++) {
43         random = r.nextInt();
44         index = Math.abs(random % len--);
45         result[i] = source[index];
46         source[index] = source[len];
47         }
48         return result;
49         
50     }
51     
52 }
本人博客均是本人学习过程中的总结。
原文地址:https://www.cnblogs.com/numberfive/p/10975751.html