关于《Java核心技术》中一个例子的疑问?

纯粹理性批评 2017-03-03 10:03:36
package com.core.java;
import java.util.*;

/**
4 * This program demonstrates array manipulation.
5 * @version 1.20 2004-02-10
6 * @author Cay Horstmann
7 */
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 yourich!");
for (int r : result)
System.out.println(r);
}
}


我想请问下 这段代码中的 numbers[r] = numbers[n - 1]; n--; 为什么就能确保每次抽到的数值不同?

谢谢各位了
...全文
177 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
moyalan7722 2017-12-20
  • 打赏
  • 举报
回复
这样子做的话:如果你抽到的是最后一个数,则删除最后一个数;如果抽到的不是最后一个数,则用最后一个数替换掉已经抽中的位置的数。就可以保证每一次抽奖得到的数字都不会相同。
南极达殇 2017-03-03
  • 打赏
  • 举报
回复
引用 4 楼 kanxingwang 的回复:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] r:9 result[0]=numbers[9]:10 [1, 2, 3, 4, 5, 6, 7, 8, 9, 20, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] r:9 result[1]=numbers[9]:20 [1, 2, 3, 4, 5, 6, 7, 8, 9, 19, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] r:10 result[2]=numbers[10]:11 [1, 2, 3, 4, 5, 6, 7, 8, 9, 19, 18, 12, 13, 14, 15, 16, 17, 18, 19, 20] r:16 result[3]=numbers[16]:17 [1, 2, 3, 4, 5, 6, 7, 8, 9, 19, 18, 12, 13, 14, 15, 16, 17, 18, 19, 20] r:4 result[4]=numbers[4]:5 [1, 2, 3, 4, 16, 6, 7, 8, 9, 19, 18, 12, 13, 14, 15, 16, 17, 18, 19, 20] 5 10 11 17 20 看出来了吧,你随机数可能取到一个,不重复的关键就是: 把你选中的那个数值的位置用数组里面其他的数字覆盖掉 确保每次抽到的数值不同
首先就是说 比如第一次是随机到9了 结果呢,numbers【9】是10 那么接下来换了什么呢?numbers【9】=20; 现在里面有两个20了啊?是吧 这时候就是n--的作用了 保证了一个事情,就是呢。 int r = (int) (Math.random() * n); 这样就不会出现20了 那就是说随机的结果变成了【1-19】 所以最后还是只有一个20 这就是巧妙地地方啊。
南极达殇 2017-03-03
  • 打赏
  • 举报
回复
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] r:9 result[0]=numbers[9]:10 [1, 2, 3, 4, 5, 6, 7, 8, 9, 20, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] r:9 result[1]=numbers[9]:20 [1, 2, 3, 4, 5, 6, 7, 8, 9, 19, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] r:10 result[2]=numbers[10]:11 [1, 2, 3, 4, 5, 6, 7, 8, 9, 19, 18, 12, 13, 14, 15, 16, 17, 18, 19, 20] r:16 result[3]=numbers[16]:17 [1, 2, 3, 4, 5, 6, 7, 8, 9, 19, 18, 12, 13, 14, 15, 16, 17, 18, 19, 20] r:4 result[4]=numbers[4]:5 [1, 2, 3, 4, 16, 6, 7, 8, 9, 19, 18, 12, 13, 14, 15, 16, 17, 18, 19, 20] 5 10 11 17 20 看出来了吧,你随机数可能取到一个,不重复的关键就是: 把你选中的那个数值的位置用数组里面其他的数字覆盖掉 确保每次抽到的数值不同
含蓄的坏 2017-03-03
  • 打赏
  • 举报
回复
numbers数组中元素是1-n,每次随机下标赋值给result数组,怎么才能保证result中K个元素不重复呢? 本程序就是让numbers数组中已经赋值给result的元素改变(或删除),方法就是每次循环把numbers的最后一个元素赋值给 已经赋值给result的那个元素,然后n--,即相当于删除最后这个元素,就能保证每次循环,numbers数组中的元素都不一样。具体令n=3,k=2看下就明白了。
南极达殇 2017-03-03
  • 打赏
  • 举报
回复
看不懂,可以自己打日志输出,就好了。 你可以看看我改的,我把输入的写死了。 去掉了无关的打印。

/**
4 * This program demonstrates array manipulation.
5 * @version 1.20 2004-02-10
6 * @author Cay Horstmann
7 */
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();
        int k=5;
       // System.out.print("What is the highest number you can draw? ");
       // int n = in.nextInt();
        int n=20;
        // 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;

        
        System.out.println(Arrays.toString(numbers));
        // 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);
            System.out.println("r:"+r);
            // pick the element at the random location
            result[i] = numbers[r];
            System.out.println("result["+i+"]="+"numbers["+r+"]:"+numbers[r]);

            // move the last element into the random location
            numbers[r] = numbers[n - 1];
            
            n--;
            System.out.println(Arrays.toString(numbers));
        }

        // print the sorted array
        Arrays.sort(result);
       // System.out.println("Bet the following combination. It'll make yourich!");
        for (int r : result)
            System.out.println(r);
    }
}


xief_1015 2017-03-03
  • 打赏
  • 举报
回复
n一直在变动,即使随机数相同,每次乘以一个变动的数,结果又怎么会一样呢
纯粹理性批评 2017-03-03
  • 打赏
  • 举报
回复
引用 5 楼 kanxingwang 的回复:
[quote=引用 4 楼 kanxingwang 的回复:] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] r:9 result[0]=numbers[9]:10 [1, 2, 3, 4, 5, 6, 7, 8, 9, 20, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] r:9 result[1]=numbers[9]:20 [1, 2, 3, 4, 5, 6, 7, 8, 9, 19, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] r:10 result[2]=numbers[10]:11 [1, 2, 3, 4, 5, 6, 7, 8, 9, 19, 18, 12, 13, 14, 15, 16, 17, 18, 19, 20] r:16 result[3]=numbers[16]:17 [1, 2, 3, 4, 5, 6, 7, 8, 9, 19, 18, 12, 13, 14, 15, 16, 17, 18, 19, 20] r:4 result[4]=numbers[4]:5 [1, 2, 3, 4, 16, 6, 7, 8, 9, 19, 18, 12, 13, 14, 15, 16, 17, 18, 19, 20] 5 10 11 17 20 看出来了吧,你随机数可能取到一个,不重复的关键就是: 把你选中的那个数值的位置用数组里面其他的数字覆盖掉 确保每次抽到的数值不同
首先就是说 比如第一次是随机到9了 结果呢,numbers【9】是10 那么接下来换了什么呢?numbers【9】=20; 现在里面有两个20了啊?是吧 这时候就是n--的作用了 保证了一个事情,就是呢。 int r = (int) (Math.random() * n); 这样就不会出现20了 那就是说随机的结果变成了【1-19】 所以最后还是只有一个20 这就是巧妙地地方啊。 [/quote]谢谢
纯粹理性批评 2017-03-03
  • 打赏
  • 举报
回复
我明白了 兄弟们 谢谢大家

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧