package DDMT.Nodes;
/*
 * MyUtil.java
 *
 * Created on 23:01, August 3rd, 2004
 */

/** Some util functions.
 * @author Kun Liu
 * @version 1.1
 */

public final class MyUtil{

   /**Returns 'num' non-duplicate random integer, uniformly distributed with values
    * between 0 (inclusive) and the specified value @upperBound (exclusive).
    */
   public static int [] randomSequence(int num, int upperBound){

       int tmpCounter=0;
       int [] randomIndex = new int[num];
       int tmpIndex;
       java.util.Random randomGenerator = new java.util.Random();
       boolean duplicate = false;

              if (num > upperBound){
                System.out.println("number of random numbers greater than the possible values!");
                return randomIndex;
              }


       while(tmpCounter < num){
         tmpIndex = randomGenerator.nextInt(upperBound);
                for (int j=0;j<tmpCounter;j++){
                   if (tmpIndex == randomIndex[j]){
                      duplicate  = true;
                      break;
                   }
                }
                if (duplicate){
                          duplicate = false;
                          continue;
                }
                else
                   randomIndex[tmpCounter++] = tmpIndex;
       }
       return randomIndex;
  }


   /**Returns 'num' non-duplicate random integer, uniformly distributed with values
    * between 0 (inclusive) and the specified value @upperBound (exclusive).
    * Furthermore, none of these random numbers should equal to 'exceptInt'
    */
   public static int [] randomSequence(int num, int upperBound, int exceptInt){

       int tmpCounter=0;
       int [] randomIndex = new int[num];
       int tmpIndex;
       java.util.Random randomGenerator = new java.util.Random();
       boolean duplicate = false;

              if (num > upperBound){
                System.out.println("number of random numbers greater than the possible values!");
                return randomIndex;
              }


       while(tmpCounter < num){
         tmpIndex = randomGenerator.nextInt(upperBound);
         if (tmpIndex == exceptInt) continue;

                for (int j=0;j<tmpCounter;j++){
                   if (tmpIndex == randomIndex[j]){
                      duplicate  = true;
                      break;
                   }
                }
                if (duplicate){
                          duplicate = false;
                          continue;
                }
                else
                   randomIndex[tmpCounter++] = tmpIndex;
      }
       return randomIndex;
  }


  /** Main function -- for testing**/
  public static void main(String [] args)
  {
          int [] randomIndex = MyUtil.randomSequence(2,5,0);
          for (int i=0; i<randomIndex.length;i++)
            System.out.print(randomIndex[i]+" ");
  }

}
