Generating Random Numbers is quite simple. Just use the class Random and method nextInt as :
new Random().nextInt(10);
This will generate a unique random number between 0 and 9 (both inclusive). Similarly if you want to generate a unique random number between a fixed length, say 10 to 100, use can use a code something like :
new Random().nextInt(90)+11;
However, while developing games or implementing Artificial Intelligence, we often require an efficient algorithm or piece of code which can generate unique random numbers within a given range.
The logic behind this is pretty simple. Take a for loop and insert all the numbers which you want to be called randomly in an ArrayList, shuffle the ArrayList and extract the numbers one by one.
The java code for this is presented below :
public class RandomNumberGenerator {
ArrayList numbersList = new ArrayList ();
public RandomNumberGenerator(int length) {
for(int x=1;x<=length;x++)
numbersList.add(x);
Collections.shuffle(numbersList);
}
public int generateNewRandom(int n) {
return numbersList.get(n);
}
}
Now you can use this class for generation of unique random numbers in your program. Just pass the required arguement in the constructor and then use the method generateNewRandom()

Posted in 

Then if won’t be possible to get the same number twice… how is it random ? (did I miss something ?)
Hum ok I had trouble with random + unique as an abjective to number. I guess you want to call an existing number from a list in a random order.
Hi Nil,
The objective is to generate unique random numbers within a given range. i.e. avoiding generation of same number twice.
e.g In a Bingo or Housie game the number caller must generate random numbers without repetition.
And it is random because you cant guess what will be the next number called.
I think the criticism (which is interesting) is that the numbers become less random the longer the function is used. (within the context of a single session). The function I use (for SAML sessions) looks like this:
private static Random random = new Random();
private static final char[] charMapping = “abcdefghijklmnop”.toCharArray();
public static String newSession() {
byte[] bytes = new byte[20]; // 160 bits
random.nextBytes(bytes);
char[] chars = new char[40];
for (int i = 0; i > 4) & 0×0f;
int right = bytes[i] & 0×0f;
chars[i * 2] = charMapping[left];
chars[i * 2 + 1] = charMapping[right];
}
return String.valueOf(chars);
}
Combined with a short lifespan and a timestamp+VMID based secret means the odds that two users would ever have the same session+secret are acceptably low. This code is taken from some SAML 2.0 reference code.
generateNewRandom looks very error prone. It would be better to implement iterator here – hasNextNumber and getNextNumber.
Agreed.
A better way can be to implement hasNextNumber first and then get (or generate) NextNumber
Possibly something like this:
public class RandomNumberGenerator implements Iterator{
private ArrayList numbersList = new ArrayList();
private int length;
private int position;
public RandomNumberGenerator(int length) {
this.length = length;
this.position = 0;
for(int x = 0; x < length; x++){
numbersList.add(Integer.valueOf(x));
}
Collections.shuffle(numbersList);
}
public boolean hasNext(){
return position < length ? true : false;
}
public Object next(){
return numbersList(++position);
}
}
This is what I was thinking too. Ideally a random number generator function should be a no-arg method.