I have a class containing an object Random. I use the object Randomas part of overloaded methods hashCode()and equals(Object o). I found that two objects java.util.Randomcreated with the same seed do not return the same hash code and do not return true.
public class RandomTest extends TestCase {
public void testRandom() throws Exception {
Random r1 = new Random(1);
Random r2 = new Random(1);
assertEquals(r1.hashCode(), r2.hashCode());
assertEquals(r1, r2);
}
}
I know that the obvious work is to use the seed plus nextSomething () for comparison (not perfect, but it should work pretty well). Therefore, my question is: why are two objects of type Random created with the same seed and at the same iteration are not equal?
source
share