# 安卓：生成无重复的随机数[英] android: generate random number without repetition

## 推荐答案

```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)
{
}
```

```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++)

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()
}
```

## 其他推荐答案

```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)
{
}
```

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++)

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());
}
```