I am receiving a sequence of roughly sequential integers (coming from a database key), and I want to map each integer to a double between 0 and 1 in a deterministic way, so that the result set of all the doubles be (close to) uniformly distributed. This should ideally be true even after only a small number of integers have been received.
The mapping needs to be deterministic because it can happen multiple times for each integer (on multiple different machines), and the resulting double should be the same each time the mapping occurs.
e.g. What should the function mapToDouble look like below?
final int start = 100000;
final int size = 2000;
final double[] results = new double[size];
for (int i = 0; i < size; i++) {
results[i] = mapToDouble(i + start)
}
// results is uniformly distributed
The best approach I can think of so far is something like:
double mapToDouble(final int i) {
final String s = new StringBuilder().append(i).append(".0").reverse().toString();
return new Double(s);
}
which is approximately uniformly distributed for the most significant bits with a relatively small sample size, but can be skewed for the less significant bits.