RandomUtil.java 587 B

1234567891011121314151617181920212223242526272829303132
  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 flag = false;
  8. int randInt = 0;
  9. for (int i = 0; i < count; i++) {
  10. do {
  11. randInt = rand.nextInt(maxNum);
  12. for (int i1 = 0; i1 < count; i1++) {
  13. if (res[i1] == randInt) {
  14. flag = false;
  15. break;
  16. }
  17. flag=true;
  18. }
  19. } while (!flag);
  20. flag = false;
  21. res[i] = randInt;
  22. }
  23. return res;
  24. }
  25. }