hectichavana
hectichavana

Reputation: 1446

android: generate random number without repetition

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

Upvotes: 2

Views: 11492

Answers (4)

Nick Banks
Nick Banks

Reputation: 4408

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

Upvotes: 2

Hakan Ozbay
Hakan Ozbay

Reputation: 4719

For what its worth, you could try using the Mersenne Twister algorithm, 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.

Upvotes: 0

Matt Kline
Matt Kline

Reputation: 10487

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.

Upvotes: 5

Haphazard
Haphazard

Reputation: 10948

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

Upvotes: 10

Related Questions