Generating random numbers with a vector of integers as seed in Java

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I want to generate reproducible random numbers representing quantities at different points in 3 dimensional space, e.g.

double draw = rand(int seed, int x, int y, int z)

I want the same inputs to always produce the same draw. I don’t want to generate all the values in advance as there would be too many.

I want the draws for different positions to be independent. I also want draws for the same position with different seeds to be independent. This rules out taking the sum or product of the four arguments, and using this as a seed.

Answers

http://download.oracle.com/javase/6/docs/api/java/util/Random.html#Random(long) http://download.oracle.com/javase/6/docs/api/java/util/Random.html#Random(long)

But you shouldn t worry much since you can apply a (mathematical) function to your vector and your seed to produce a single number. A poor man s version would be simply adding the numbers:

 Random rand = new Random(seed+x+y+z);

But as you probably noticed yourself, that isn t the best one as it yields the same result for (1,0,0) and (0,1,0) and (0,0,1). I am sure you can think up a better function instead like seed + 31*31*x + 31*y + z or similar.

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/5929859/generating-random-numbers-with-a-vector-of-integers-as-seed-in-java

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils