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

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

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

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

例如 :

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 timesCollections.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


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5507093.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-13
下一篇 2022-12-13

发表评论

登录后才能评论

评论列表(0条)

保存