1

I have the following class:

public class MyClass
{
    private Random rand;
    private HashSet<Pair<Integer, Integer>> set;

    public MyClass()
    {
       rand = new Random(Double.doubleToLongBits(Math.random()));
       set = new HashSet<Pair<Integer, Integer>>();
    }


    public void doSomething(int len)
    {   
        set.clear();

        for (int i = 0; i < 1000; i++)
        {
            int index = rand.nextInt(len - 1) + 1;

            int min = 1 - index;
            int max = len - index - 1;
            int j = rand.nextInt(max - min + 1) + min;

            if (j != 0)
            {   
               set.add(new Pair<Integer, Integer>(index, j));
            }
        }
    }
}

Pair is a custom class where I can store two integers. The problem is that every time I call doSomething() the HashSet contains always the same values.

How is it possible? How can I fix this problem?

EDIT:

this is my Pair: https://stackoverflow.com/a/677248/1395740

Community
  • 1
  • 1
Nick
  • 10,309
  • 21
  • 97
  • 201

3 Answers3

0

From the documentation:

An instance of this class is used to generate a stream of pseudorandom numbers. The class uses a 48-bit seed, which is modified using a linear congruential formula. (See Donald Knuth, The Art of Computer Programming, Volume 3, Section 3.2.1.)

If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers.

Joachim Rohde
  • 5,915
  • 2
  • 29
  • 46
  • That's what I thought about but the seed is itself random (`new Random(Double.doubleToLongBits(Math.random()))`). I looked at the implementation of Random and `new Random()` uses `System.nanotime()`. – Arnaud Denoyelle Dec 09 '13 at 15:34
0

Math.random returns a double and Random receives a long as its seed. So probably you are getting 0 as the seed everytime. Try to change to System.getNanoTime() just for a test and see what happens.

prmottajr
  • 1,816
  • 1
  • 13
  • 22
0

Regarding to your code, when run it with 5 as len parameter it's result this: [(4, -2), (4, -1), (4, -3), (2, -1), (3, -1), (3, -2), (1, 1), (2, 1), (1, 2), (3, 1), (2, 2), (1, 3)] [(4, -2), (3, -1), (4, -1), (4, -3), (3, -2), (2, -1), (1, 1), (2, 1), (1, 2), (3, 1), (2, 2), (1, 3)]

if you trace your code, it's not same pair, every time doSomething method generate new random numbers, but in loop , all numbers that lead j !=0 will ignored, so you will have that pairs,

your problem in this code

    int index = rand.nextInt(len - 1) + 1;
    int min = 1 - index;
    int max = len - index - 1;
    int j = rand.nextInt(max - min + 1) + min;

    if (j != 0)
    {   
       set.add(new Pair<Integer, Integer>(index, j));
    }

i mean this code leads to got same number regardless what random number be.

Gladiator
  • 264
  • 1
  • 5
  • So the solution to fix the problem is...? – Nick Dec 09 '13 at 17:42
  • this code will generate all probabilities of two integers (pair) where it's less than len (method parameter), so every time you will have same pair with deference order , solution will be regarding to your business , – Gladiator Dec 11 '13 at 11:51