import java.io.FileWriter;
import java.io.IOException;

/**
 * Generate test data
 * <p>
 * Example output
 * c1_x c1_y
 * c2_x c2_y
 * ...
 * ck_x ck_y
 * <p>
 * d1_x d1_y
 * d2_y d2_y
 * ...
 * dszie_x dsize_y
 */
public class DataGenerator {


    /**
     * Random generate centroids
     *
     * @param n          the number of centroids
     * @param d          the dimensional of centroids
     * @param lowerBound the lower bound of the random range
     * @param upperBound the upper bound of the random range
     * @return a 2d double array represents the list of centroids
     */
    public static double[][] generateCentroids(int n, int d, double lowerBound, double upperBound) {
        double[][] centroids = new double[n][d];
        double range = upperBound - lowerBound;

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < d; j++) {
                centroids[i][j] = lowerBound + range * Math.random();
            }
        }

        return centroids;
    }

    /**
     * Random generate test centroids, 2d, 10~90
     *
     * @param n the amount of centroids
     * @return a 2d double array represents the list of the centroids
     */
    public static double[][] generateCentroids(int n) {
        return generateCentroids(n, 2, 10, 90);
    }

    /**
     * Random generate data
     *
     * @param size      the amount of data
     * @param centroids the seed centroids
     * @param radius    the radius round the seeds
     * @return a 2d double array represents data
     * @throws EmptyDataSetException if input the centroid array is empty.
     */
    public static double[][] generateData(int size, double[][] centroids, double radius) throws EmptyDataSetException {
        if (centroids == null || centroids.length == 0 || centroids[0].length == 0)
            throw new EmptyDataSetException("The input centroids array is empty");
        double[][] data = new double[size][centroids[0].length];

        double r = radius * 2;
        int t = 0; // index of centroid
        for (int i = 0; i < size; i++) {
            t = i % centroids.length;
            for (int j = 0; j < data[0].length; j++) {
                data[i][j] = centroids[t][j] - radius + r * Math.random();
            }
        }

        return data;

    }

    /**
     * Random generate test data
     *
     * @param size      the amount of data
     * @param centroids the seed centroids
     * @return a 2d double array represents data
     * @throws EmptyDataSetException if input the centroid array is empty
     */
    public static double[][] generateData(int size, double[][] centroids) throws EmptyDataSetException {
        return generateData(size, centroids, 10);
    }

    /**
     * Output a data set to the specified file
     * @param data the data set
     * @param out the output file
     * @throws IOException if any error caught when write data
     */
    private static void output(double[][] data,FileWriter out) throws IOException {
        if(data.length==0) return;
        for (int i = 0; i < data[0].length; i++) {
            out.write(data[0][i]+" ");
        }
        out.write("\n");

        for (int i = 1; i < data.length; i++) {
            for (int j = 0; j < data[0].length; j++) {
                out.write(data[i][j]+" ");
            }
            out.write("\n");
        }

    }

    /**
     * Main method
     *
     * @param args k size out_file_name
     */
    public static void main(String[] args) {
        int k = Integer.parseInt(args[0]); // num of centroids
        int size = Integer.parseInt(args[1]); // size of data

        double[][] centroids = generateCentroids(k);
        double[][] data = null;
        try {
            data = generateData(size, centroids);
        } catch (EmptyDataSetException e) {
            e.printStackTrace();
        }

        try {
            FileWriter out = new FileWriter(args[2]);
            output(data,out);
            out.close();

            out = new FileWriter(args[2]+"_centroids");
            output(centroids,out);
            out.close();

        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}

@@ -1050,12 +1050,10 @@
 d, 1
-0
 ~9
-0
 %0A   
@@ -1274,13 +1274,11 @@
 2, 1
-0
 , 9
-0
 );%0A 
@@ -2698,17 +2698,16 @@
 roids, 1
-0
 );%0A    %7D
@@ -2759,24 +2759,31 @@
 cified file%0A
+     *%0A
      * @para
@@ -2820,16 +2820,17 @@
 ram out 
+ 
 the outp
@@ -2955,16 +2955,17 @@
 %5B%5D data,
+ 
 FileWrit
@@ -3003,16 +3003,17 @@
       if
+ 
 (data.le
@@ -3016,18 +3016,20 @@
 a.length
+ 
 ==
+ 
 0) retur
@@ -3043,125 +3043,316 @@
     
-for (int i = 0; i %3C data%5B0%5D.length; i++) %7B%0A            out.write(data%5B0%5D%5Bi%5D+%22 %22);%0A        %7D%0A        out.write(%22%5Cn%22);%0A
+out.write(output(data));%0A%0A    %7D%0A%0A    /**%0A     * Convert a 2d double array to a string%0A     * @param array the input 2d double array%0A     * @return a string object represents the output%0A     */%0A    public static String output(double%5B%5D%5B%5D array) %7B%0A        if (array.length == 0) return%22%22;%0A        String r = %22%22;
 %0A   
@@ -3373,19 +3373,20 @@
 i = 
-1
+0
 ; i %3C 
-data
+array
 .len
@@ -3421,36 +3421,37 @@
 (int j = 0; j %3C 
-data
+array
 %5B0%5D.length; j++)
@@ -3473,33 +3473,28 @@
     
-out.write(data
+r+=array
 %5Bi%5D%5Bj%5D
-+
+ + 
 %22 %22
-)
 ;%0A  
@@ -3521,23 +3521,15 @@
     
-out.write(
+r+=
 %22%5Cn%22
-)
 ;%0A  
@@ -3532,24 +3532,67 @@
 ;%0A        %7D%0A
+        return r.substring(0,r.length()-1);
 %0A    %7D%0A%0A    
@@ -4169,16 +4169,17 @@
 ut(data,
+ 
 out);%0A  
@@ -4242,17 +4242,19 @@
 (args%5B2%5D
-+
+ + 
 %22_centro
@@ -4289,16 +4289,17 @@
 ntroids,
+ 
 out);%0A  

create output method Changes to be committed: modified: src/main/java/DataGenerator.java