package quant.rich.emoney.util;
import java.util.*;

public class ChunkRandomIter {
    
    final static Random RANDOM = new Random();

    public static <T> List<T> splitShuffleAndFlatten(List<T> list, int n) {
        int size = list.size();
        if (n <= 0) throw new IllegalArgumentException("n must be > 0");
        n = Math.min(n, size == 0 ? 1 : size);

        List<T> parts = new ArrayList<>(n);

        int base = size / n;      // 每份至少 base 个
        int extra = size % n;     // 前 extra 份多一个

        int idx = 0;
        for (int i = 0; i < n; i++) {
            int partSize = base + (i < extra ? 1 : 0);
            List<T> part = new ArrayList<>(list.subList(idx, idx + partSize));
            Collections.shuffle(part, RANDOM);
            parts.addAll(part);
            idx += partSize;
        }
        return parts;
    }
}