Java中从[1,36]随机生成7个不重复的数字,放入一个数组中

http://bbs.csdn.net/topics/360247647

 

import java.util.Random;

public class Test {
 public static void main(String[] args) throws Exception {
  int[] a = new int[36];
  int[] b = new int[7];
  for(int i = 0;i   a[i]=i+1;
  }
  Random r = new Random();
  for(int i=0;i<7;i++){
   int x=r.nextInt(36-i);
   b[i] = a[x];
   int t = a[x];
   a[x]=a[a.length-1-i];
   a[a.length-1-i]=t;
  }
  for(int x :b){
   System.out.print(x+" ");
  }
 }
}

转载于:https://www.cnblogs.com/lteal/archive/2013/05/13/3075982.html

你可能感兴趣的:(Java中从[1,36]随机生成7个不重复的数字,放入一个数组中)