code: public static KMeans lloyd(double[][] data, int k, int maxIter, int runs) {
          if (k < 2) {
              throw new IllegalArgumentException("Invalid number of clusters: " + k);
          }
  
          if (maxIter <= 0) {
              throw new IllegalArgumentException("Invalid maximum number of iterations: " + maxIter);
          }
  
          if (runs <= 0) {
              throw new IllegalArgumentException("Invalid number of runs: " + runs);
          }
  
          KMeans best = lloyd(data, k, maxIter);
  
          for (int i = 1; i < runs; i++) {
              KMeans kmeans = lloyd(data, k, maxIter);
              if (kmeans.distortion < best.distortion) {
                  best = kmeans;
              }
          }
  
          return best;
      }
docs: The implementation of Lloyd algorithm as a benchmark. Run the algorithm
  multiple times and return the best one in terms of smallest distortion.
  The data may contain missing values (i.e. Double.NaN).
  @param data the input data of which each row is a sample.
  @param k the number of clusters.
  @param maxIter the maximum number of iterations for each running.
  @param runs the number of runs of K-Means algorithm.