简体   繁体   English

如何生成重复次数> 0的固定数量的随机数

[英]How to generate random numbers with a set number of duplicates > 0

有没有一种特定的方法来生成随机数,并且仅使用Random在Java中获取每个数字的x份重复项?

No. 没有。

There is no way to use the Random API to generate a list of numbers with a user-defined duplicate limit. 无法使用Random API生成具有用户定义的重复限制的数字列表。 You're best bet is to generate the numbers (with Random) and then discard duplicates after they appear more than x times. 最好的办法是生成数字(使用随机数),然后在重复项出现超过x次后丢弃重复项。 You could use a duplicate supporting multimap implementation to do this, maybe com.google.common.collect.ArrayListMultimap 您可以使用重复的支持多图实现来执行此操作,例如com.google.common.collect.ArrayListMultimap

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM