jeudi 4 juin 2015

Java random shuffle list with two elements using Collections.shuffle

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;

public class ShuffleList {
  public static void main(String[] args) {
    String [] file = {"1","2"};
    long seed = 3;
    ArrayList<String> fileList = new ArrayList<String>(Arrays.asList(file));
    for (int i=0; i<200; i++) {
      Collections.shuffle(fileList, new Random(seed));
      seed = seed +1;
      System.out.println(seed + "," + fileList);
    }
  }
}

The output is 200 lines of [1,2], not random shuffle at all. In fact this is true for all seed < 4000. Why is that? I tried with a list of 3 elements and seeds from 1 to 100 makes the list seemingly random. But what's wrong with a list of 2 elements?




Aucun commentaire:

Enregistrer un commentaire