如何拥有独特的随机数?

这就是我如何在1到6之间生成一个唯一的no并从drawable文件夹中获取适当的图像。

Random rand = new Random(); // n = the number of images, that start at idx 1 rndInt = rand.nextInt(6) + 1; String imgName = "card" + rndInt; int id = getResources().getIdentifier(imgName, "drawable", getPackageName()); imgView.setImageResource(id); 

我想要的是,我必须调用这个方法7次,每次这个方法都应该返回一个唯一的随机数。 所以没有一个已经选择的数字会再次出现。

解决此类问题的常用方法是创建一个包含每个可能值的列表并将其随机化(使用Collections.shuffle )。 然后,每次需要值时,从列表中使用一个项目。 这将确保您不会多次使用相同的值,但仍允许随机顺序。

这是一个使用Dan Dyer建议的方法创建随机排列的示例类。 它确保每个.next()调用给出一个新的数字,直到构造函数中给出的数字。 之后,它会缠绕并再次给出相同的序列。 这对于改组播放列表非常有用。

 import java.util.ArrayList; import java.util.Collections; import java.util.List; public class RandomPermutation{ private List list; private int index; /** * Create a random permutation the sequence of numbers 0, 1, ... , n - 1. * @param n Range specifier, must be positive */ public RandomPermutation(int n) { if (n <= 1) { throw new IllegalArgumentException( "Positive number expected, got: " + n); } list = new ArrayList(); newList(n); } /** * Creates a new list */ public void newList(int n) { index = -1; list.clear(); for (int i = 0; i < n; i++) { list.add(i); } Collections.shuffle(list); } /** * Retrieve the next integer in sequence. */ public int next() { index = (index + 1) % list.size(); return list.get(index); } } 

顺便说一句。 不要使用Snake使用的方法。 这不仅仅是因为一旦使用了所有数字它就会冻结。 这可以修复。 问题更多的是程序运行越来越慢,因为listIdontWantAnymore中的数字越来越多。 只有6个数字不是问题,但如果范围很大,它可能会导致相当大的减速。 考虑选择10000个数字。 选择了9900个数字后,有1%的机会击中一个好的数字。 在9990之后,有0.1%的机会击中一个好的数字等。

以下是如何使用该类的示例:

 static RandomPermutation randomPerm = new RandomPermutation(7) int NextRandomNumber() { return randomPerm.next() + 1; } 

这是最简单的代码,可以存储到数组中而不会重复:

 Random rand = new Random(); int[] ar; ar = new int[5]; int random = 0; int [] result_arr=new int[5]; boolean flag=false; for(int i=0;i<5;i++) { ar[i]=0; do{ random =rand.nextInt(10); flag=false; for(int j=0;j 

这将在数​​组中创建唯一的数字

对于您的特定用例,这应该可以解决问题。

 Random rand = new Random(); // n = the number of images List imgNames = new ArrayList(n); for (int i = 0; i < n; i++) { imgNames.add("card" + (i + 1)) } while (!imageNames.isEmpty()) { String imgName = imgNames.remove(rand.next(imageNames.size()); int id = getResources().getIdentifier(imgName, "drawable", getPackageName()); imgView.setImageResource(id); } 

请注意,当n变大时,这不能很好地扩展。 对于ArrayListLinkedList ,remove操作是O(n) 。 但是对于数百或数千的n ,与加载和显示图像相比,这可能是微不足道的。

此外,正如评论所指出的“独特随机数”在术语上是矛盾的。 你所追求的是从1n的数字集的随机排列 。 我的解决方案在没有明确的“改组”步骤的情况下为您提供了这一点,这对您的用例来说已经足够了。

生成包含您将使用的每个数字的数字列表。 (这很好,因为我们正在谈论一个小范围,其中“N”是“不到一千的地方”)

选择数字时,选择0和sizeof(列表)之间的随机索引,该数字将成为该列表的索引。

删除该列表索引并返回该号码。

(这是练习者在这里确定哪种“列表”适合的练习。)

使用具有适当选择参数的线性同余生成器 。

创建一个您已经获得的可能性的静态列表。

 static ArrayList listIdontWantAnymore = new ArrayList(); int NextRandomNumber() { Random random = new Random(); int myRandomInt; do { myRandomInt = random.NextInt(6) + 1; } while(listIdontWantAnymore.Contains(myRandomInt)); listIdontWantAnymore.Add(myRandomInt); // now your 'myRandomInt' is what you want it to be. return myRandomInt; }