首页 > 编程语言 > 详细

将一个数组随机打乱

时间:2015-07-03 01:37:43      阅读:168      评论:0      收藏:0      [点我收藏+]
 public int []  shuffle(int [] arr) {
      int [] arr2 =new int[arr.length];
      int count = arr.length;
      int cbRandCount = 0;// 索引
      int cbPosition = 0;// 位置
      int k =0;
      do {
       Random rand = new Random();
       int r = count - cbRandCount;
       cbPosition = rand.nextInt(r); 
       arr2[k++] = arr[cbPosition];
       cbRandCount++;
       arr[cbPosition] = arr[r - 1];// 将最后一位数值赋值给已经被使用的cbPosition
      } while (cbRandCount < count);
      return arr2;
     }

 

将一个数组随机打乱

原文:http://www.cnblogs.com/xuhuaiqu/p/4617562.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!