用数组中最后元素覆盖随机抽取到的位置,这样的算法真的合适么?

在Java核心技术一书(我用第八版)中有一个实例(P79),用来完成:

产生一个抽彩游戏中的随机数组合。

其中,实现防止重复抽中用的算法是:

         // r是随机数,不会超界;n由numbers数组的length得道
         numbers[r] = numbers[n - 1];
         n--;

我没看懂这个算法是怎么做到防止重复抽中的。请劳烦详细解释下,谢谢。

FYI:

代码:

import java.util.*;

/**
 * This program demonstrates array manipulation.
 * @version 1.20 2004-02-10
 * @author Cay Horstmann
 */
public class LotteryDrawing
{
   public static void main(String[] args)
   {
      Scanner in = new Scanner(System.in);

      System.out.print("How many numbers do you need to draw? ");
      int k = in.nextInt();

      System.out.print("What is the highest number you can draw? ");
      int n = in.nextInt();

      // fill an array with numbers 1 2 3 . . . n
      int[] numbers = new int[n];
      for (int i = 0; i < numbers.length; i++)
         numbers[i] = i + 1;

      // draw k numbers and put them into a second array
      int[] result = new int[k];
      for (int i = 0; i < result.length; i++)
      {
         // make a random index between 0 and n - 1
         int r = (int) (Math.random() * n);

         // pick the element at the random location
         result[i] = numbers[r];

         // move the last element into the random location
         numbers[r] = numbers[n - 1];
         n--;
      }

      // print the sorted array
      Arrays.sort(result);
      System.out.println("Bet the following combination. It'll make you rich!");
      for (int r : result)
         System.out.println(r);
   }
}
阅读 3.8k
1 个回答

这是一个数组内部取随机值后换位置的算法,每次取数组中的随机数后,把这个随机数与数组尾部的值换位,这样取过的值全部移动到数组尾部,而新的随机值会在0和n-1之间,也就是从头部到最后一个未换位的位置之间取,因此不会有重复值出现.
比如:
numbers = [0,1,2,3,4,5],
假如r=2,则取出一个随机值numbers[2] ,也就是数字2,
然后进行换位,numbers[r] = numbers[n - 1],2被换到数组最后一位,数组此时变成:
[0,1,5,3,4,2],
此时5换到了前面,这时n--后,再次取随机值时是从[0,1,5,3,4,2]中取了(加粗部分),所以新的随机值必定不会包含已经取出的2.
同理,再次取值时,这个值会放到倒数第二位.

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题