您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何从不互相关注的数字池中生成随机数

如何从不互相关注的数字池中生成随机数

是的,有一些简短的方法可以实现您所描述的内容

例如 :

Integer[] arr = {3,6,7,11,20};
List<Integer> shuffled = new ArrayList<>();
for (Integer i : arr)
    shuffled.addAll (Collections.nCopies(x,i)); // add i to your List x times
Collections.shuffle(shuffled); // shuffle the List to get random order

或者(如果您不想使用Collections.nCopies(x,i)):

Integer[] arr = {3,6,7,11,20};
List<Integer> shuffled = new ArrayList<>();
for (int j = 0; j < x; j++)
    for (Integer i : arr)
        shuffled.add (i);
Collections.shuffle(shuffled); // shuffle the List to get random order
其他 2022/1/1 18:28:27 有670人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶