0

I would like to add some random behavior to my code. I want to do it with the timestamp value. By this way, I want to shuffle a list with the current timestamp. This is my code:

Random random = new Random(SimClock.getIntTime());
Collections.shuffle(list, random);

Shuffle works correctly if I don't use random var to shuffle the list. However, when I use the code above the output list is always the same (it is never shuffled).

Random value changes every time Collections.shuffle is called (I checked it), so I don't know why my list is not shuffled when I use the random value. Why is this happening?

Update

list is an ArrayList with this value: [MIX0, MIX1].

This is the value of the list and of the random var when shuffle is called three times in the same execution:

randon value: 25214903885   
List value before shuffle: [MIX0, MIX1]
List value after shuffle: [MIX0, MIX1]

randon value: 25214903895   
List value before shuffle: [MIX0, MIX1]
List value after shuffle: [MIX0, MIX1]

randon value: 25214903865   
List value before shuffle: [MIX0, MIX1]
List value after shuffle: [MIX0, MIX1]

The random value is copied from Eclipse Variables values.

When I use the shuffle method without random value the list is shuffled sometimes by this way: [MIX1, MIX0].

Miguel.G
  • 377
  • 1
  • 6
  • 20
  • 3
    `SimClock` makes it sound like a *simulated* clock, so perhaps it always returns the **same** simulated time? If you seed the `Random` object with the same value each time, it will always return the same sequence of "random" values, which means the `shuffle` method will always shuffle in exactly the same order. --- Please provide [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example), i.e. show a sample input `list`, show the value returned by `SimClock.getIntTime()`, and show the result of the shuffle. Show how it doesn't work for you. – Andreas Nov 02 '19 at 16:17
  • 1
    what is the "expected" output when timestamp is passed as the argument? – AADProgramming Nov 02 '19 at 16:31
  • Possible duplicate of [Random generator giving me the same number everytime](https://stackoverflow.com/questions/13899537/random-generator-giving-me-the-same-number-everytime) – Ole V.V. Nov 02 '19 at 20:26

1 Answers1

0

I used following code:

public static void main(String[] args) throws IOException {
   List<String> arr = new ArrayList<>(3);
   arr.add("aaa");
   arr.add("bbb");
   arr.add("ccc");

   for (int i = 0; i < 4; i++) {
       Collections.shuffle(arr, new Random(System.currentTimeMillis()));
       System.out.println(ArrayUtils.toString(arr));
   }
}

out:
[ccc, aaa, bbb]
[bbb, ccc, aaa]
[aaa, bbb, ccc]
[ccc, aaa, bbb]

When in argument of new Random(...) you put constant value, all out put will be same.

hbi640
  • 11
  • 2
  • Better than in the question. However, if your code runs fast enough, there may not pass a full millisecond until the next time through the loop, and you will have the same problem again. Best to create one `Random` object outside the loop and use it every time. If this is not possible, second best is to create the `Random` object without specifying seed:: just `new Random()`. – Ole V.V. Nov 06 '19 at 04:58