模拟一个shuffle

之所以会想到写这么一个shuffle的例子,是因为一个需求:我需要把一个有序数组中的数据随机的打散。java代码如下,

public void shuffle()
{
  int[] arr = {1,2,3,4,5,6,7,8,9,10};
  
  Random random = new Random();
  random.setSeed(System.currentMillions());    
  
  for(int i = 0; i < 10; i++)      
  {    
    int index = Math.abs(random.nextInt())%(n-i)+i;
    if(index!=i)
   {
      int temp = arr[i];  
      arr[i] = ar[index];
      arr[index]=temp;
    }
   } 
    System.out.println(Arrays.toString(arr))
}    
原文地址:https://www.cnblogs.com/OliverZhang/p/6550285.html