java 根据最小值和最大值生成指定个数不重复的随机数(指定n个数量)

代码随机生成count个不重复的随机数,平均数约(max-min)/count。

目前有遍历list、遍历int[]、使用HashSet去重list返回、使用set去重int[]返回、使用LinkedHashSet

首先list效率肯定比int[]低,所以遍历list、HashSet+List一定比另2个低。LinkedHashSet返回时需要转成list或int[]用处不大

所以推荐使用的有:

①如果count都小于100,则遍历int[]效率及内存最优

②如果count有大于100的,推荐HashSet+int[]组合(如果不清楚,推荐使用此方式)

遍历list效率一直是最差的,不推荐使用

    /**
     * 随机count个不重复数据,包含min和max
     * HashSet+int[]组合,推荐使用
     */
    public static int[] randomNoRepeatSetInt(int min, int max, int count) {
        if (count < 1) {
            throw new IllegalArgumentException("count必须大于0");
        }
        if (max + 1 - min < count) {
            throw new IllegalArgumentException("范围必须大于count,不然怎么不重复?");
        }
        Random random = new Random();
        HashSet<Integer> set = new HashSet<>(count);
        int[] ints = new int[count];
        while (set.size() < count) {
            int next = nextNum(random, min, max);
            if (set.add(next)) {
                ints[set.size() - 1] = next;
            }
        }
        return ints;
    }

    /**
     * 随机count个不重复数据,包含min和max
     * 遍历int[],count<100时内存效率最优
     */
    public static int[] randomNoRepeatInt(int min, int max, int count) {
        if (count < 1) {
            throw new IllegalArgumentException("count必须大于0");
        }
        if (max + 1 - min < count) {
            throw new IllegalArgumentException("范围必须大于count,不然怎么不重复?");
        }
        Random random = new Random();
        int[] ints = new int[count];//此处也可以直接用int数组
        int size = 0;
        continueThis:
        while (size < count) {
            int next = nextNum(random, min, max);
            for (int i = 0; i < size; i++) {
                if (ints[i] == next) {
                    continue continueThis;//继续while
                }
            }
            ints[size] = next;
            size++;
        }
        return ints;
    }

    /**
     * 随机count个不重复数据,包含min和max
     * 遍历List,效率一直最差,不推荐使用
     */
    public static List<Integer> randomNoRepeatList(int min, int max, int count) {
        if (count < 1) {
            throw new IllegalArgumentException("count必须大于0");
        }
        if (max + 1 - min < count) {
            throw new IllegalArgumentException("范围必须大于count,不然怎么不重复?");
        }
        Random random = new Random();
        ArrayList<Integer> list = new ArrayList<>(count);
        while (list.size() < count) {
            int next = nextNum(random, min, max);
            if (!list.contains(next)) {
                list.add(next);
            }
        }
        return list;
    }

    private static int nextNum(Random random, int min, int max) {
        return random.nextInt(max + 1 - min) + min;
    }

当然你也可以只用list然后遍历判断是否包含,感觉上节约个set对象,但你可以试试20个以上的数据我打包票上面最快 

随机单条数据:

    /**
     * 范围内随机一个值,包含min和max
     */
    public static int nextNum(int min, int max) {
        if (max <= min) {
            throw new IllegalArgumentException("max必须大于min");
        }
        return nextNum(new Random(), min, max);//见上面的方法
    }

可重复的就不多说了吧,直接fori然后nextNum就行了

 

如果想平均数也要定制,见另一篇博客:https://blog.csdn.net/weimingjue/article/details/103698334

 

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值