java android:生成不重复的随机数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5807512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
android: generate random number without repetition
提问by hectichavana
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.
任何人都可以帮助我制作一种在Android中不重复生成随机数的方法吗?最大数量是:prjcts.size();
这是我的 JSON 数组。并且返回值应该是整数。
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.
我已经拥有的是:
int i = (int)(prjcts.size() * Math.random());
我将方法施放了 3 次,因为我需要 3 个随机生成的数字。它有效,但我不知道如何在不重复的情况下制作它。所以这三个数字彼此之间不会相同。
Thank you
谢谢
回答by Haphazard
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()
}
回答by Matt Kline
Have you tried just using Math.random()?
你试过只使用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.
randomIndex
现在保证是您的 JSON 列表中从未使用过的索引。
回答by Nick Banks
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.
获得 N 个随机数而不从 0 到 N-1 重复的一种方法是创建一个由这些 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());
}
回答by Hakan Ozbay
For what its worth, you could try using the Mersenne Twisteralgorithm, which has a Java implementation of it here. The Mersenne Twister is a random number generator which has a period of 2^19937 ? 1, so you're pretty much guaranteed to not get the same random number.
对于它的价值,您可以尝试使用Mersenne Twister算法,该算法在此处具有 Java 实现。Mersenne Twister 是一个周期为 2^19937 的随机数发生器?1,所以你几乎可以保证不会得到相同的随机数。