Example usage for java.util Collections shuffle

List of usage examples for java.util Collections shuffle

Introduction

In this page you can find the example usage for java.util Collections shuffle.

Prototype

public static void shuffle(List<?> list) 

Source Link

Document

Randomly permutes the specified list using a default source of randomness.

Usage

From source file:br.com.autonomiccs.autonomic.plugin.common.services.AutonomiccsSystemVmDeploymentService.java

/**
 * This method looks for a random host in the given zone ( {@link DataCenterVO}) to deploy an Autonomiccs system VM.
 * It loads all pods ( {@link HostPodVO}) of the environment, and then it randomizes the list and tries to look for suitable hosts using {@link #searchForRandomHostInPodToDeployAutonomiccsSystemVm(HostPodVO)}
 *
 * @return {@link HostVO} to deploy the system VM, it can also return null if no suitable hosts were found.
 *//*from   ww w.j  ava  2  s  . co m*/
private HostVO searchForRandomHostInZoneToDeployAutonomiccsSystemVm(DataCenterVO dataCenterVO) {
    List<HostPodVO> allPodsEnabledFromZone = podService.getAllPodsEnabledFromZone(dataCenterVO.getId());
    Collections.shuffle(allPodsEnabledFromZone);
    for (HostPodVO hostPodVO : allPodsEnabledFromZone) {
        HostVO host = searchForRandomHostInPodToDeployAutonomiccsSystemVm(hostPodVO);
        if (host != null) {
            return host;
        }
    }
    logger.info(String.format(
            "Could not find any suitable hosts to deploy the system VM into zone [zoneId=%d, zoneName=%s]",
            dataCenterVO.getId(), dataCenterVO.getName()));
    return null;
}

From source file:fr.ritaly.dungeonmaster.item.ItemManager.java

@Override
public Sector getRandomPlace() {
    if (items == null) {
        return null;
    }/*from  w w w. j  a  v  a 2s  . c  o  m*/

    final List<Sector> sectors = new ArrayList<Sector>(items.keySet());

    if (sectors.isEmpty()) {
        // Not supposed to happen
        return null;
    }

    Collections.shuffle(sectors);

    return sectors.iterator().next();
}

From source file:net.sourceforge.subsonic.service.sonos.SonosHelper.java

public List<AbstractMedia> forShuffleArtist(int mediaFileId, int count, String username,
        HttpServletRequest request) {/*  ww w  . j  a va 2s. c o m*/
    MediaFile artist = mediaFileService.getMediaFile(mediaFileId);
    List<MediaFile> songs = filterMusic(mediaFileService.getDescendantsOf(artist, false));
    Collections.shuffle(songs);
    songs = songs.subList(0, Math.min(count, songs.size()));
    return forMediaFiles(songs, username, request);
}

From source file:fr.fg.server.contract.NpcHelper.java

/**
 * Renvoie un prnom fminin alatoire.//from w  w w  .j  a v a 2 s. co  m
 * 
 * @return Un prnom fminin alatoire.
 */
public static String getRandomFemaleFirstName() {
    String value = femaleFirstNames.get(currentFemaleFirstName).getName();
    currentFemaleFirstName++;

    if (currentFemaleFirstName == femaleFirstNames.size()) {
        Collections.shuffle(femaleFirstNames);
        currentFemaleFirstName = 0;
    }

    return value;
}

From source file:org.flite.cach3.test.ReadThroughMultiCacheTest.java

@Test
public void testVelocity() {
    final Long constant = RandomUtils.nextLong();

    final List<Long> firsts = new ArrayList<Long>();
    final List<Long> thrus = new ArrayList<Long>();
    final Long f1 = System.currentTimeMillis();
    firsts.add(f1);/*from w w  w.j  a  v  a  2  s .c o m*/

    final Long base = f1 + RandomUtils.nextInt(1000) + 500;
    for (int ix = 0; ix < 4; ix++) {
        firsts.add(base + ix);
        thrus.add(base + ix);
    }
    Collections.shuffle(firsts);

    final String early = RandomStringUtils.randomAlphanumeric(8);
    final String late = RandomStringUtils.randomAlphanumeric(12);

    final List<String> bodies = new ArrayList<String>();
    final List<String> keys = new ArrayList<String>();
    final List<String> subs = new ArrayList<String>();
    for (int ix = 0; ix < firsts.size(); ix++) {
        if (firsts.get(ix).equals(f1)) {
            bodies.add(early);
        } else {
            bodies.add(late);
            subs.add(late);
            keys.add(firsts.get(ix).toString() + "&&" + constant);
        }
    }

    final TestSvc test = (TestSvc) context.getBean("testSvc");
    final StubReadThroughMultiCacheListenerImpl listener = (StubReadThroughMultiCacheListenerImpl) context
            .getBean("stubRTM");

    // This just primes the cache so we can ensure the sub-set feature works in the multi-cache example.
    test.getCompoundString(f1, early, constant);

    // Get the before count of objects that have been triggered, so we can isolate if the call triggers.
    final int previous = listener.getTriggers().size();

    final List<String> results = test.getCompoundStrings(firsts, late, constant);
    for (int ix = 0; ix < results.size(); ix++) {
        assertEquals(bodies.get(ix), results.get(ix));
    }

    // Testing that the listener got invoked as required.
    assertTrue("Doesn't look like the listener got called.", listener.getTriggers().size() == previous + 1);

    final String expected = StubReadThroughMultiCacheListenerImpl.formatTriggers(TestDAOImpl.COMPOUND_NAMESPACE,
            TestDAOImpl.COMPOUND_PREFIX, keys, (List<Object>) (List) subs, // Using Erasure to satisfy the compiler. YUCK!
            null, new Object[] { thrus, late, constant });
    LOG.info("Expected = " + expected);
    assertEquals(expected, listener.getTriggers().get(listener.getTriggers().size() - 1));
}

From source file:ml.dmlc.xgboost4j.java.XGBoost.java

private static List<Integer> genRandPermutationNums(int start, int end) {
    List<Integer> samples = new ArrayList<Integer>();
    for (int i = start; i < end; i++) {
        samples.add(i);//w  w w  .ja  va2s .  c o m
    }
    Collections.shuffle(samples);
    return samples;
}

From source file:fr.fg.server.contract.NpcHelper.java

/**
 * Renvoie un nom de famille alatoire./*  w ww  . j  a  va  2 s  . c  o m*/
 * 
 * @return Un nom de famille alatoire.
 */
public static String getRandomLastName() {
    String value = lastNames.get(currentLastName).getName();
    currentLastName++;

    if (currentLastName == lastNames.size()) {
        Collections.shuffle(lastNames);
        currentLastName = 0;
    }

    return value;
}

From source file:com.hackedcube.blenderopenmovies.VideoDetailsFragment.java

private void setupMovieListRow() {
    String subcategories[] = { getString(R.string.related_movies) };
    List<Movie> list = MovieList.list;

    Collections.shuffle(list);
    ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(new CardPresenter());
    for (int j = 0; j < NUM_COLS; j++) {
        listRowAdapter.add(list.get(j % 4));
    }//from  ww w  .ja va 2  s .  com

    HeaderItem header = new HeaderItem(0, subcategories[0]);
    mAdapter.add(new ListRow(header, listRowAdapter));
}

From source file:com.mgmtp.jfunk.data.generator.util.GeneratingExpression.java

/**
 * Replaces randomly selected characters in the given string with forbidden characters according
 * to the given expression.//from w  ww  .  ja v  a 2  s  .  co m
 * 
 * @param bad
 *            if bad = -2 all characters are replaced, if bad = -1 1 - (all - 1) are replaced,
 *            if bad > 0 the exact number of characters is replaced
 * @param input
 *            the input string
 * @return input with <code>bad</code> replaced characters
 */
public String negateString(final String input, int bad) {
    int length = input.length();
    Range[] ranges = getRanges();
    int[] lengths = new int[ranges.length];
    // arrange lengths
    for (int i = 0; i < lengths.length; i++) {
        Range r = ranges[i];
        lengths[i] = r.getMin();
        length -= r.getMin();
    }
    /**
     * distribute remaining lengths
     */
    int i = 0;
    // only 1000 tries otherwise break as it just does not work
    while (length > 0 && i < 1000) {
        int index = i % lengths.length;
        Range r = ranges[index];
        if (lengths[index] < r.getMax()) {
            lengths[index] += 1;
            length--;
        }
        i++;
    }

    // generate completely negative string
    String replaceString = generate(lengths, -2);
    if (replaceString.length() == 0) {
        log.warn("No negative characters possible in this expression. All characters are allowed.");
        return input;
    }
    // now replace the #bad character in the input string
    List<Integer> l = new ArrayList<Integer>(input.length());
    for (i = 0; i < input.length(); i++) {
        l.add(i);
    }
    if (bad == -2) {
        // all false
        bad = input.length();
    } else if (bad == -1) {
        bad = 1 + random.getInt(input.length() - 1);
    }
    Collections.shuffle(l);
    StringBuffer base = new StringBuffer(input);
    int j = 0;
    for (i = 0; i < bad; i++) {
        int index = l.remove(0);
        char replaceChar = ' ';
        if (index < replaceString.length()) {
            replaceChar = replaceString.charAt(index);
        }
        while ((index == 0 || index >= replaceString.length() || index == input.length() - 1)
                && Character.isSpaceChar(replaceChar)) {
            replaceChar = replaceString.charAt(j);
            j = (j + 1) % replaceString.length();
        }
        base.setCharAt(index, replaceChar);
    }
    if (log.isDebugEnabled()) {
        log.debug("False characters in string; " + input + " became " + base);
    }
    return base.toString();
}