问题描述
任何人都可以帮助我制作一种方法来生成随机数而不会在Android中重复的方法吗? 最大数字是:prjcts.size();这是我的JSON数组.返回值应该在整数中.
我已经拥有的是: int i = (int)(prjcts.size() * Math.random());我施放了该方法3次,因为我需要3个随机生成的数字.它有效,但我不知道如何在没有重复的情况下进行.因此,这3个数字彼此之间不会相同.
谢谢
推荐答案
您是否尝试过仅尝试使用 math.random()?
只是做一些铸造魔术,您会很好地走:
int index = (int)((double)prjcts.size() * Math.random());
编辑:
如果要防止重复,可以创建一个带有所有可能索引的列表.
int max = prjcts.size(); List<int> indices = new ArrayList<int>(max); for(int c = 0; c < max; ++c) { indices.add(c); }
然后,每次您想要一个随机索引,只需从列表中选择一个随机项目,然后将其从列表中删除
后将其从列表中删除int arrIndex = (int)((double)indices.size() * Math.random()); int randomIndex = indices.get(arrIndex); indices.remove(arrIndex);
randomIndex现在保证是JSON列表的索引以前从未使用过.
其他推荐答案
我在您的另一个问题中提到了如何做到这一点.
List<Integer> list = new ArrayList<Integer>(); int jsonMax = prjcts.size(); for(int i = 1; i<=jsonMax; i++) list.add(i); Collections.shuffle(list); for(int i=0; i<jsonMax; i++) { int n = list.get(i); //n is a random, unique number between 1 and prjcts.size() }
其他推荐答案
获得N随机数,并从0到N-1重复n随机数的一种方法是创建这些n个数字的数组,并创建一个随机数,该数字会选择该数组的一个索引.然后从该数组中删除索引,然后继续使用N-1号等.
class NoRepeatRandom { private int[] number = null; private int N = -1; private int size = 0; public NoRepeatRandom(int minVal, int maxVal) { N = (maxVal - minVal) + 1; number = new int[N]; int n = minVal; for(int i = 0; i < N; i++) number[i] = n++; size = N; } public void Reset() { size = N; } // Returns -1 if none left public int GetRandom() { if(size <= 0) return -1; int index = size * Math.random(); int randNum = number[index]; // Swap current value with current last, so we don't actually // have to remove anything, and our list still contains everything // if we want to reset number[index] = number[size-1]; number[--size] = randNum; return randNum; } } void Test() { NoRepeatRandom nrr = new NoRepeatRandom(0, 10); for(int i = 0; i < 12; i++) System.out.println("Random number: " + nrr.GetRandom()); }
问题描述
can anybody help me in making a method to generate random number without repetition in Android? The maximum number is: prjcts.size(); it's my JSON Array. And the return value should be in integer.
What I've already had is: int i = (int)(prjcts.size() * Math.random()); I casted the method 3 times, because I need 3 random generated numbers. It works, but I don't know how to make it without repetition. So those 3 numbers won't be the same between each other.
Thank you
推荐答案
Have you tried just using Math.random()?
Just do some casting magic and you'll be good to go:
int index = (int)((double)prjcts.size() * Math.random());
Edit:
If you want prevent repetition, you could create a list with all the possible indices.
int max = prjcts.size(); List<int> indices = new ArrayList<int>(max); for(int c = 0; c < max; ++c) { indices.add(c); }
Then each time you want a random index, just pick a random item from the list, removing it after from the list when you're done
int arrIndex = (int)((double)indices.size() * Math.random()); int randomIndex = indices.get(arrIndex); indices.remove(arrIndex);
randomIndex is now guaranteed to be a never-before used index of of your JSON list.
其他推荐答案
I mentioned in your other question how to do this..
List<Integer> list = new ArrayList<Integer>(); int jsonMax = prjcts.size(); for(int i = 1; i<=jsonMax; i++) list.add(i); Collections.shuffle(list); for(int i=0; i<jsonMax; i++) { int n = list.get(i); //n is a random, unique number between 1 and prjcts.size() }
其他推荐答案
One way to get N random numbers with out repeat from 0 to N-1 would be to create an array of those N numbers and create a random number that will pick one index of that array. Then remove the index from that array, and continue on with the N-1 numbers and so on.
class NoRepeatRandom { private int[] number = null; private int N = -1; private int size = 0; public NoRepeatRandom(int minVal, int maxVal) { N = (maxVal - minVal) + 1; number = new int[N]; int n = minVal; for(int i = 0; i < N; i++) number[i] = n++; size = N; } public void Reset() { size = N; } // Returns -1 if none left public int GetRandom() { if(size <= 0) return -1; int index = size * Math.random(); int randNum = number[index]; // Swap current value with current last, so we don't actually // have to remove anything, and our list still contains everything // if we want to reset number[index] = number[size-1]; number[--size] = randNum; return randNum; } } void Test() { NoRepeatRandom nrr = new NoRepeatRandom(0, 10); for(int i = 0; i < 12; i++) System.out.println("Random number: " + nrr.GetRandom()); }