I think this is random enough for your purpose
The idea is to stuff the random numbers into a HashSet as you go along.
When the size of the hashset is 10 you know that it contains 10
unique integers.
Code:
import java.util.*;
public class Ramdomizer {
public static Random rand=new Random(System.currentTimeMillis());
static HashSet hs=new HashSet();
public Ramdomizer() {}
public int [] getRandoms(int maxNo) {
hs.clear();
while (true) {
int n = rand.nextInt(maxNo);
hs.add(new Integer(n));
if (hs.size() == maxNo) {
break;
}
}
int [] ret=new int[maxNo];
Iterator it=hs.iterator();
int cnt=0;
while (it.hasNext()) {
ret[cnt++]=((Integer)it.next()).intValue();
}
return shuffle(ret);
}
private int [] shuffle(int [] arr) {
for (int i=0; i<arr.length; i++) {
int rp = rand.nextInt(10);
int tmp=arr[rp];
arr[rp]=arr[i];
arr[i]=tmp;
}
return arr;
}
/**
* *********** MAIN ***********
*/
public static void main(String[] args) {
Ramdomizer rd = new Ramdomizer();
int [] rn=rd.getRandoms(10);
showResult(rn);
rn=rd.getRandoms(10);
showResult(rn);
}
private static void showResult(int[] rn) {
for (int i=0; i<rn.length; i++) {
System.out.println(i+": "+rn[i]);
}
}
}