Java Collections.shuffle Examples

Java Collections.shuffle - 30 examples found. These are the top rated real world Java examples of java.util.Collections.shuffle extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
  private static List<FoodSchedule> schedule(
      List<String> breakfastParticipants,
      List<String> snackParticipants,
      List<DateTime> fridaysAvailable) {
    // shuffle both lists
    Collections.shuffle(breakfastParticipants);
    List<String> modifiableBreakfast = new ArrayList<String>(breakfastParticipants);
    Collections.shuffle(snackParticipants);
    List<String> modifiableSnack = new ArrayList<String>(snackParticipants);

    // while not empty, schedule
    int participantCount = breakfastParticipants.size();

    List<FoodSchedule> schedule = new ArrayList<FoodSchedule>();
    for (int i = 0; i < participantCount; i += 2) {
      String bp1 = modifiableBreakfast.remove(0);
      String bp2 = modifiableBreakfast.remove(0);
      String sp1 = modifiableSnack.remove(0);
      String sp2 = modifiableSnack.remove(0);
      DateTime friday = fridaysAvailable.remove(0);
      FoodSchedule fs = new FoodSchedule();
      fs.setDateTime(friday);
      fs.setBreakfastParticipantIds(Arrays.asList(bp1, bp2));
      fs.setSnackParticipantIds(Arrays.asList(sp1, sp2));
      schedule.add(fs);
    }
    return schedule;
  }
  /** Give it many different graphs and see if it can reconstruct the order correctly */
  @Test
  public void exhaustiveValid() {
    for (int numRows = 2; numRows <= 10; numRows++) {
      for (int numCols = 2; numCols <= 10; numCols++) {
        List<QuadBlob> expected = createBlobs(numCols, numRows);

        // randomize the order
        List<QuadBlob> input = new ArrayList<QuadBlob>();
        input.addAll(expected);
        Collections.shuffle(input, rand);

        for (QuadBlob b : input) {
          Collections.shuffle(b.conn, rand);
        }

        OrderChessboardQuadBlobs alg = new OrderChessboardQuadBlobs(numCols, numRows);
        assertTrue(numCols + " " + numRows, alg.order(input));

        // check the node order to see if it's as expected
        List<QuadBlob> found = alg.getResults();
        assertEquals(expected.size(), found.size());
        for (int i = 0; i < input.size(); i++) {
          assertTrue(expected.get(i) == found.get(i));
        }
      }
    }
  }
Example #3
0
  // make 2-letter options with 'vokal' as second letter
  private String generateAutoOptionsVokal(int nOptions, String answer, String avoid) {

    String vokaler = "A,E,I,O,U,Y,Æ,Ø,Å";
    ArrayList<String> vokalerList = new ArrayList<String>(Arrays.asList(vokaler.split(",")));

    String c0 = answer.substring(0, 1);
    String c1 = answer.substring(1, 2);
    Collections.shuffle(vokalerList);

    ArrayList<String> options = new ArrayList<String>();
    options.add(answer);

    if (nOptions > vokalerList.size()) nOptions = vokalerList.size();

    int i = 0;
    while (options.size() < nOptions) {

      // add if different from answer
      if (!c1.equals(vokalerList.get(i))) options.add(c0 + vokalerList.get(i));

      i++;
    }

    Collections.shuffle(options);
    return joinArray(options);
  }
Example #4
0
 protected Iterator<CachedAccount> getRuleAwareIterator() {
   if (rules == null) {
     return cache.iterator();
   }
   ArrayList<CachedAccount> orderedCache = new ArrayList<AccountCache.CachedAccount>(cache);
   int startRandom = -1;
   for (int index = 0; index < orderedCache.size(); index++) {
     Rules rule = rules.get(index);
     if (rule == null) {
       if (startRandom >= 0) {
         Collections.shuffle(orderedCache.subList(startRandom, index));
         startRandom = -1;
       }
       continue;
     }
     switch (rule) {
       case RANDOM:
         if (startRandom < 0) {
           startRandom = index;
         }
         break;
       default:
         if (startRandom >= 0 && index - startRandom > 1) {
           Collections.shuffle(orderedCache.subList(startRandom, index));
         }
         startRandom = -1;
         break;
     }
   }
   if (startRandom >= 0) {
     Collections.shuffle(orderedCache.subList(startRandom, orderedCache.size()));
   }
   return orderedCache.iterator();
 }
  /**
   * add 20 shuffled rows
   *
   * @throws InterruptedException
   */
  private void load(Client client)
      throws NoConnectionsException, ProcCallException, IOException, InterruptedException {
    int pkey = 0;
    a_int.clear();
    a_inline_str.clear();
    a_pool_str.clear();

    boolean async = true;

    for (int i = 0; i < 20; i++) {
      a_int.add(i);
      a_inline_str.add("a_" + i);
      a_pool_str.add(simpleString + i);
    }

    Collections.shuffle(a_int);
    Collections.shuffle(a_inline_str);
    Collections.shuffle(a_pool_str);

    for (int i = 0; i < 20; i++) {
      SyncCallback cb = new SyncCallback();
      client.callProcedure(
          cb, "InsertO1", pkey, a_int.get(i), a_inline_str.get(i), a_pool_str.get(i));

      if (!async) {
        cb.waitForResponse();
        VoltTable vt = cb.getResponse().getResults()[0];
        assertTrue(vt.getRowCount() == 1);
      }
      pkey = pkey + 1;
    }

    client.drain();
  }
Example #6
0
  /**
   * Factory method for a list of engines and processes.
   *
   * @param engines a list of engines to be included in the test suite
   * @param processes a list of processes to be included in the test suite
   * @param testFolderName
   * @return a test suite where each engine tests all passed processes
   */
  public static BPMNTestSuite createTests(
      List<AbstractBPMNEngine> engines, final List<BPMNProcess> processes, String testFolderName) {

    BPMNTestSuite test = new BPMNTestSuite();
    test.setPath(Paths.get(testFolderName));

    for (AbstractBPMNEngine engine : engines) {
      List<BPMNProcess> clonedProcesses =
          processes.stream().map(BPMNProcess::createCopyWithoutEngine).collect(Collectors.toList());

      // link them
      for (BPMNProcess process : clonedProcesses) {
        process.setEngine(engine);
        engine.getProcesses().add(process);
      }

      // set parentFolder
      engine.setParentFolder(test.getPath());
    }

    test.setEngines(engines);
    test.setProcessesCount(getProcessesCount(engines));

    Collections.shuffle(engines);
    for (AbstractBPMNEngine engine : engines) {
      Collections.shuffle(engine.getProcesses());
    }

    return test;
  }