All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cat.nyaa.nyaacore.utils.MathUtils Maven / Gradle / Ivy

There is a newer version: 3.12.2
Show newest version
package cat.nyaa.nyaacore.utils;

import com.google.common.collect.ImmutableList;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public final class MathUtils {
    public static final int MILLI_IN_SEC = 1000;
    private static final Random rng = new Random();

    /**
     * Generate a random number in [min,max]
     */
    public static int uniformRangeInclusive(int minInclusive, int maxInclusive) {
        if (maxInclusive < minInclusive) throw new IllegalArgumentException();
        return rng.nextInt(maxInclusive - minInclusive + 1) + minInclusive;
    }

    /**
     * Select n from candidates. Return all candidates if n > candidates.size()
     * Order is not preserved.
     */
    public static  ImmutableList randomSelect(List candidates, int n) {
        if (candidates == null || n < 0) throw new IllegalArgumentException();
        if (n > candidates.size()) n = candidates.size();
        if (n == 0) return ImmutableList.of();
        if (n == candidates.size()) return ImmutableList.copyOf(candidates);
        List clone = new ArrayList<>(candidates);
        Collections.shuffle(clone, rng);

        ImmutableList.Builder b = ImmutableList.builder();
        for (int i = 0; i < n; i++) b.add(clone.get(i));
        return b.build();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy