RandomUtil.java 616 B

1234567891011121314151617181920212223
  1. package com.goafanti.common.utils;
  2. import java.util.Random;
  3. public class RandomUtil {
  4. public static int[]generateRandomList(int maxNum,int count){
  5. int[] res=new int[count];
  6. Random rand = new Random();//新建一个随机类
  7. boolean[] bool = new boolean[maxNum];
  8. int randInt = 0;
  9. for(int i = 0; i < count ; i++) {
  10. do {
  11. randInt = rand.nextInt(maxNum);//产生一个随机数
  12. }while(bool[randInt]);
  13. bool[randInt] = true;
  14. System.out.println(randInt);
  15. res[i]=randInt;
  16. }
  17. return res;
  18. }
  19. }