Why two random objects created with the same seed give different results from hashcode ()

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()); //nope
        assertEquals(r1, r2); //nope
    }
}

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?

+4
source share
2 answers

java.util.Random equals() hashCode(), - Object, . , 2 -, .

+11

, Random . Random , . . , , , .

0

Source: https://habr.com/ru/post/1584299/


All Articles