// Path: src/main/java/com/github/phantomthief/failover/util/RandomListUtils.java
// static <T> List<T> getRandomUsingLcg(List<T> source, int size) {
//     int targetSize = min(source.size(), size);
//     List<T> newList = new ArrayList<>(targetSize);
//     LcgRandomIterator<T> iterator = new LcgRandomIterator<>(source);
//     for (int i = 0; i < targetSize; i++) {
//         newList.add(iterator.next());
//     }
//     return newList;
// }
// 
// Path: src/main/java/com/github/phantomthief/failover/util/RandomListUtils.java
// static <T> List<T> getRandomUsingShuffle(Collection<T> source, int size) {
//     List<T> newList = new ArrayList<>(source);
//     shuffle(newList, ThreadLocalRandom.current());
//     return newList.subList(0, min(newList.size(), size));
// }


import static com.github.phantomthief.failover.util.RandomListUtils.getRandomUsingLcg;
import static com.github.phantomthief.failover.util.RandomListUtils.getRandomUsingShuffle;
import static java.lang.System.nanoTime;
import static java.util.stream.Collectors.toList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.IntStream;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
package com.github.phantomthief.failover.util;




/**
 * @author w.vela
 * Created on 2018-02-26.
 */
class RandomListUtilsTest {

    private static final Logger logger = LoggerFactory.getLogger(RandomListUtilsTest.class);

    @Test
    void getRandom() {
        long lcgBetter = 0, shuffleBetter = 0;
        for (int i = 0; i < 1000; i++) {
            int size = ThreadLocalRandom.current().nextInt(1, 10000);
            int retrieved = ThreadLocalRandom.current().nextInt(1, Math.max(2, size / 3));
            List<Integer> list = IntStream.range(0, size).boxed().collect(toList());

            long s = nanoTime();
            List<Integer> result = getRandomUsingLcg(list, retrieved);
            long costLcg = nanoTime() - s;

            long s2 = nanoTime();
            getRandomUsingShuffle(list, retrieved);
            long costShuffle = nanoTime() - s2;

            assertEquals(retrieved, result.stream().distinct().count());
            if (costLcg > costShuffle) {
                shuffleBetter += costLcg - costShuffle;
            } else {
                lcgBetter += costShuffle - costLcg;
            }
        }
        logger.info("lcg better:{}, shuffle better:{}", lcgBetter, shuffleBetter);
        lcgBetter = 0;
        shuffleBetter = 0;
// Path: src/main/java/com/github/phantomthief/failover/util/RandomListUtils.java
// static <T> List<T> getRandomUsingLcg(List<T> source, int size) {
//     int targetSize = min(source.size(), size);
//     List<T> newList = new ArrayList<>(targetSize);
//     LcgRandomIterator<T> iterator = new LcgRandomIterator<>(source);
//     for (int i = 0; i < targetSize; i++) {
//         newList.add(iterator.next());
//     }
//     return newList;
// }
// 
// Path: src/main/java/com/github/phantomthief/failover/util/RandomListUtils.java
// static <T> List<T> getRandomUsingShuffle(Collection<T> source, int size) {
//     List<T> newList = new ArrayList<>(source);
//     shuffle(newList, ThreadLocalRandom.current());
//     return newList.subList(0, min(newList.size(), size));
// }


// Path: src/test/java/com/github/phantomthief/failover/util/RandomListUtilsTest.java
import static com.github.phantomthief.failover.util.RandomListUtils.getRandomUsingLcg;
import static com.github.phantomthief.failover.util.RandomListUtils.getRandomUsingShuffle;
import static java.lang.System.nanoTime;
import static java.util.stream.Collectors.toList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.IntStream;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

package com.github.phantomthief.failover.util;




/**
 * @author w.vela
 * Created on 2018-02-26.
 */
class RandomListUtilsTest {

    private static final Logger logger = LoggerFactory.getLogger(RandomListUtilsTest.class);

    @Test
    void getRandom() {
        long lcgBetter = 0, shuffleBetter = 0;
        for (int i = 0; i < 1000; i++) {
            int size = ThreadLocalRandom.current().nextInt(1, 10000);
            int retrieved = ThreadLocalRandom.current().nextInt(1, Math.max(2, size / 3));
            List<Integer> list = IntStream.range(0, size).boxed().collect(toList());

            long s = nanoTime();
            List<Integer> result = getRandomUsingLcg(list, retrieved);
            long costLcg = nanoTime() - s;

            long s2 = nanoTime();
            getRandomUsingShuffle(list, retrieved);
            long costShuffle = nanoTime() - s2;

            assertEquals(retrieved, result.stream().distinct().count());
            if (costLcg > costShuffle) {
                shuffleBetter += costLcg - costShuffle;
            } else {
                lcgBetter += costShuffle - costLcg;
            }
        }
        logger.info("lcg better:{}, shuffle better:{}", lcgBetter, shuffleBetter);
        lcgBetter = 0;
        shuffleBetter = 0;
        for (int i = 0; i < 1000; i++) {