package project1;

import java.util.*;

import java.io.*;

 

 

/**

* Title: Project #1 ICS 311

* Description: Random permutations and Running time analysis

* Copyright: Copyright (c) 2001

* Company: n/a

* @author Stacie Unebasami, Section 2

* @author Jeffrey Ming-Yun Wu, Section 1

* @version 1.0

*/

 

public class Project1 {

 

int[] randomArray;

int[][] times_array;

Random random = new Random();

final static int ARRAY_SIZE = 10;

final static int RANGE = 10;

 

 

public static void main(String[] args) throws IOException {

 

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

Project1 proj = new Project1();

boolean duplicate = false;

 

 

System.out.println("You are now entering the first generateData... ");

System.out.println("How many times would you like to run the program " +

"(-1 to exit): ");

int times = Integer.parseInt(br.readLine());

 

 

while (times != -1) {

 

long startingTime = System.currentTimeMillis(); //starts clock

 

proj.times_array = new int[times][ARRAY_SIZE];

 

for (int outer = 0; outer < times; outer++) {

proj.generateData(); // generate data & put in randomArray

// proj.printer();

if (outer == 0) {

//theres nothing in that array so no need to compare

//put the random array into times_array

for (int i = 0; i < proj.randomArray.length; i++) {

proj.times_array[outer][i] = proj.randomArray[i];

} // end for

} // end if

 

else {

int countOut;

 

boolean added = false;

 

for (countOut = outer - 1; countOut >= 0; countOut--) {

duplicate = false;

//System.out.println(" countout " + countOut + " outer: " + outer);

 

for (int i2 = 0; i2 < proj.randomArray.length && !duplicate; i2++) {

//System.out.println(proj.times_array[countOut][i2] + " " + proj.randomArray[i2]);

if (proj.times_array[countOut][i2] != proj.randomArray[i2]) {

//stop the presses! its not a duplicate.

//check previous arrays for a duplicate.

duplicate = true;

} // end if

 

} // end for

} // end for

 

for (int i = 0; i < proj.randomArray.length; i++) {

proj.times_array[outer][i] = proj.randomArray[i];

} //end for

 

} // end else

} // end for

long endingTime = System.currentTimeMillis();

long elapsedTimeInMillis = (endingTime - startingTime);

//System.out.println();

 

System.out.println("times " + elapsedTimeInMillis);

 

 

System.out.println("How many times would you like to run the program " +

"(-1 to exit): ");

times = Integer.parseInt(br.readLine());

} // end while

// proj.printer();

/*

long startingTime2 = System.currentTimeMillis();

proj.generateData2();

long endingTime2 = System.currentTimeMillis();

long elapsedTimeInMillis2 = (endingTime2 - startingTime2);

System.out.println(elapsedTimeInMillis2);

// proj.printer();

 

 

long startingTime3 = System.currentTimeMillis();

proj.generateData3();

long endingTime3 = System.currentTimeMillis();

long elapsedTimeInMillis3 = (endingTime3 - startingTime3);

System.out.println(elapsedTimeInMillis3);

// proj.printer();

*/

}

 

public void generateData() {

boolean found = false;

randomArray = new int[ARRAY_SIZE];

int index, index2;

 

for (index = 0; index < randomArray.length; index++) {

found = false;

int randomInt = random.nextInt(RANGE); // random number

// System.out.print(randomInt + " ");

 

if (index == 0) { //nothing inside so nothing to compare to, stick it in

randomArray[index] = randomInt;

found = true;

}

for (index2 = 0; index2 < index && !found; index2++) {

if (randomArray[index2] == randomInt) {

index--;

found = true;

}

else if (index2 == index-1 && !found) {

randomArray[index] = randomInt;

} // end else if

} // end for

} // end for

} // end method

 

public void generateData2() {

randomArray = new int[ARRAY_SIZE];

boolean[] usedArray = new boolean[RANGE];

for (int index = 0; index < randomArray.length; index++) {

int randomInt = random.nextInt(RANGE);

// System.out.print(randomInt + " ");

if (usedArray[randomInt] == false) {

randomArray[index] = randomInt;

usedArray[randomInt] = true;

}

else {

index--;

}

}

}

 

public void generateData3() {

boolean found = false;

randomArray = new int [ARRAY_SIZE];

int index;

int temp;

int newPos;

 

for (index = 0; index < randomArray.length; index++) {

randomArray[index] = (index + 1);

}

 

for (index = 1; index < randomArray.length; index++) {

newPos = randInt(0, index);

temp = randomArray[newPos];

randomArray[newPos] = randomArray[index];

randomArray[index] = temp;

}

 

 

}

 

public int randInt(int a, int b) {

return (int)((b - a + 1)*Math.random() + a);

}

public void printer() {

for (int indexx = 0; indexx < randomArray.length; indexx++) {

if (indexx == 0){

System.out.println();

} // end if

System.out.print("Array: " + randomArray[indexx] + " ");

} // end for

} //end method

 

public void printer2() {

int i = 0;

for (int indexx = 0; indexx < times_array.length; indexx++) {

if (indexx == 0){

System.out.println();

} // end if

 

System.out.print("Array: " + times_array[indexx][i] + " ");

i++;

 

if (i == 10) {

i = 0;

}

} // end for

} //end method

 

} // end class