MADDY
MADDY

Reputation: 147

how to put data into array of integer

I am new to Android and also to java. in my application exactly what i need is random numbers.which will not be repeated once again.

for an example Say i have a TextView and a Button on clicking the button it should show me random numbers and those random numbers shoud not be repeated.

for this i was thinking to store the random data into array and check whether the random number is in the array or not .if it is not there in the array it will store that number for temporally .like on first click it should show me some random number like in between 1-20 say it selected 5, now this value should be stored into array and on clicking the next button if again 5 number come then it should skip this number .

Please help me regarding this,

Upvotes: 1

Views: 1538

Answers (5)

Stephen Quan
Stephen Quan

Reputation: 25956

Create an array with your numbers in sequence. Then use the Fisher-Yates shuffle algorithm to generate your unique, non repeating random sequence.

// Fisher-Yates shuffle.
Random random = new Random();
int[] data = new int[20];
for (int i=0; i<20; i++)
{
  data[i] = i;
}
for (int i=0; i<20; i++)
{
  int j = random.nextInt(20);
  int t = data[i];
  data[i] = data[j];
  data[j] = t;
}

Upvotes: 1

anubhava
anubhava

Reputation: 785156

Talking about the data structure to store your generated random consider using java.util.Set for storing unique set of numbers. TO check whether a number already exists in Set use Set#contains(Object o) method.

CODE:

int max = 20;
System.out.println("Generating random integers in range 0.." + max);
Set<Integer> set = new HashSet<Integer>(max);
Random randomGenerator = new Random();
while (set.size() <= max) {
  int randomInt = randomGenerator.nextInt(max+1);
  System.out.println("Generated : " + randomInt);
  if (!set.add(randomInt))
      System.out.println("Set already has: " + randomInt);
}
System.out.println("Set has: " + set);

Upvotes: 1

Ovidiu Latcu
Ovidiu Latcu

Reputation: 72321

You should create first an instance of Random. This will generate for you random numbers. Then you should keep all your already generated numbers in an ArrayList<Integer> so you can keep track of your generated numbers so far.

Random mRandom=new Random();
//Generate first number, int [0,20)
int no=mRandom.nextInt(20);
//keep that number
ArrayList<Integer> mGeneratedSoFar=new ArrayList<Integer>();
mGeneratedSoFar.add(no);

then in your button click handler, generate a number that was not generated until now :

int no=mRandom.nextInt(20);
while(mGeneratedSoFar.contains(no)){
    no=mRandmom.nextInt(20);
}
mGeneratedSoFar.add(no);

Edit: and as @Guido Garcia said, you can keep your numbers in other types of Collection or in Maps for better performance. But as long as you generate few numbers, I don't think this will be an issue.

Upvotes: 1

z12345
z12345

Reputation: 2216

java.util.Random generator;    
java.util.Set<Integer> nums;
nums.add(generator.nextInt(20));

Upvotes: 0

Guido
Guido

Reputation: 47665

I think this question is related with Java, not with Android.

If you only need to create a small set of unique random numbers, you can store the generated numbers in a Map, so you can determine in O(1) if a number was generated before. If it was not generated before, use it and put it into the map. Else create a new one and repeat.

Upvotes: 0

Related Questions