Q:

Unique random number for a particular timestamp

I am kind of learning concepts of Random number generation & Multithreading in java.  
The idea is to not generating a repeated random number of range 1000 in a particular millisecond (Considering, not more than 50 data, in a multithreaded way will be processed in a millisecond).  So that list of generated random number at the specific time is unique.  Can you give me any idea as i am ending up generating couple of repeated random numbers (also, there is a considerable probability) in a particular milli second.  
I have tried the following things where i failed.  
Random random = new Random(System.nanoTime());
double randomNum = random.nextInt(999);

//
int min=1; int max=999;
double randomId = (int)Math.abs(math. Random()* (max - min + 1) + min);

//
Random random = new Random(System.nanoTime()); // also tried new Random();
double randomId = (int)Math.abs(random.nextDouble()* (max - min + 1) + min);

As I am appending the timestamp that is being generated, in a multithreaded environment i see the same ids (around 8-10) that is being generated (2-4 times) for 5000+ unique data.  

A:

First, you should use new Random(), since it looks like this (details depend on Java version): 
 public Random() { this(++seedUniquifier + System.nanoTime()); }
 private static volatile long seedUniquifier = 8682522807148012L;

I.e. it already makes use of nanoTime() and makes sure different threads with the same nanoTime() result get different seeds, which new Random(System.nanoTime()) doesn't. 
(EDIT: Pyranja pointed out this is a bug in Java 6, but it's fixed in Java 7:
public Random() {
    this(seedUniquifier() ^ System.nanoTime());
}

private static long seedUniquifier() {
    // L'Ecuyer, "Tables of Linear Congruential Generators of
    // Different Sizes and Good Lattice Structure", 1999
    for (;;) {
        long current = seedUniquifier.get();
        long next = current * 181783497276652981L;
        if (seedUniquifier.compareAndSet(current, next))
            return next;
    }
}

private static final AtomicLong seedUniquifier
    = new AtomicLong(8682522807148012L);

)
Second, if you generate 50 random numbers from 1 to 1000, the probability some numbers will be the same is quite high thanks to the birthday paradox. 
Third, if you just want unique ids, you could just use AtomicInteger counter instead of random numbers.  Or if you want a random part to start with, append a counter as well to guarantee uniqueness. 

A:

This class will allow you to get nonrepeating values from a certain range until the whole range has been used.  Once the range is used, it will be reinitialized. 
Class comes along with a simple test. 
If you want to make the class thread safe, just add synchronized to nextInt() declaration. 
Then you can use the singleton pattern or just a static variable to access the generator from multiple threads.  That way all your threads will use the same object and the same unique id pool. 
public class NotRepeatingRandom {
    int size;
    int index;
    List<Integer> vals;
    Random gen = new Random();

    public NotRepeatingRandom(int rangeMax) {
      size = rangeMax;
      index = rangeMax; // to force initial shuffle
      vals = new ArrayList<Integer>(size);
      fillBaseList();
    }

    private void fillBaseList() {
      for (int a=0; a<size; a++) {
        vals.add(a);
      }
    }

    public int nextInt() {
      if (index == vals.size()) {
          Collections.shuffle(vals);
          index = 0;
      }
      int val = vals.get(index);
      index++;      
      return val;
    }

    public static void main(String[] args) {
        NotRepeatingRandom gen = new NotRepeatingRandom(10);
        for (int a=0; a<30; a++) {
            System.out.println(gen.nextInt());
        }
    }
}

