mardi 30 juillet 2019

Array of eight Integers where each Integer has a difference of at least 15 from all others in the array?

I am trying to write a method that creates and returns an array of random Integers that has a length of eight.

Every Integer must be higher or lower than each other Integer in the array by at least 15.

I set up a main method that checks the output of my method 100,000 times, and throws an Exception if there are any Integers that are too close.

How can I create a method that will return an array of Integers with a difference between each other of at least 15?

public class Test {

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

   Integer[] distances = new Integer[8];

   for (int i = 0; i < 100000; i++) {

      distances = createPlanetDistances(distances.length);

      // check distances for values within 15
      for (int x = 0; x < distances.length; x++) {

         for (int y = 0; y < distances.length; y++) {

            if (x == y)
               continue;

            if (Math.abs(distances[x] - distances[y]) < 15) {

               System.out.println(distances[x] + " " + distances[y]);
               throw new Exception("Doesn't work");
            }
         }
      }

      for (int distance : distances)
         System.out.print(distance + " ");

      System.out.println(System.lineSeparator());
   }
}

/**
 * Creates an array of distances-from-the-sun for a given number of Planets.
 * It does not allow distances to be within 15 of any other distance.
 *
 * @param planetAmount The number of distances to return.
 * @return An array of distances-from-the-sun.
 */
private static Integer[] createPlanetDistances(int planetAmount) {

   SplittableRandom random = new SplittableRandom();

   final int min = 25;
   final int max = 726;

   HashSet<Integer> distanceSet = new HashSet<>();

   // make sure there are no duplicate Integers

   for(int i = 0; i < planetAmount; i++) {

      int num = random.nextInt(min, max);

      while (distanceSet.contains(num))
         num = random.nextInt(min, max);

      distanceSet.add(num);
   }

   // make sure each distance is 15 away from all others

   Integer[] distances = distanceSet.toArray(new Integer[]{});

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

      // check distances[i] with all other Integers
      for (int j = 0; j < distances.length; j++) {

         // do not compare an element with itself
         if (j == i)
            continue;

         int difference = Math.abs(distances[i] - distances[j]);

         if (difference < 15) {

            while (difference < 15) {

               distances[i] = random.nextInt(min, max);
               difference = Math.abs(distances[i] - distances[j]);
            }

            // check all Integers again
            System.out.println("HERE " + i + " " + j);
            i = 0;
            break;
         }
      }
   }

   return distanceSet.toArray(new Integer[]{});
}
}




Aucun commentaire:

Enregistrer un commentaire