package genesom;

import java.util.Random;

public class ShuffleIndex
{

  public long seed;

  int ind;
  int max;
  int[] shuffle_index;
  Random random;


  public ShuffleIndex(int noind) {
    this(noind, System.currentTimeMillis());
  }

  public ShuffleIndex(int noind, long sd) {
    max = noind;
    seed = sd;
System.err.println("# rand(ShuffleIndex.java) = {"+ seed + "}");
    shuffle_index = new int[max];
    reset();
  }


  public void reset() {
    random = new Random(seed);
    ind = Integer.MAX_VALUE-1;
    for(int i = 0; i < max; i++) shuffle_index[i] = i;
  }


  public void reshuffle()
  {

    int ind, tmp;

    for(int cnt = 0; cnt < max; cnt++) {
      ind = (int)(random.nextDouble() * max);
      tmp = shuffle_index[cnt];
      shuffle_index[cnt] = shuffle_index[ind];
      shuffle_index[ind] = tmp;
    }
  }



  public int next()
  {

    if(++ind >= max) {
      reshuffle();
      ind = 0;
    }

    return shuffle_index[ind];
  }

}
