export class Random {
    static random() {
        return Math.random();
    }
    static withinRange(a, b) {
        let d = b - a;
        return a + Random.random() * d;
    }
    static IntegerWithinRange(a, b) {
        return Math.round(Random.withinRange(a, b));
    }
    static chooseFromArray(arr) {
        return arr[Math.floor(Random.random() * arr.length)];
    }
    static int(min, max) {
        return Math.floor(Random.random() * (max - min + 1)) + min;
    }
    static weightedChoice(weights, divisor = 1) {
        let adjustedWeights = weights.map(weight => weight / divisor);
        let sum = adjustedWeights.reduce((a, b) => a + b, 0);
        let rand = Random.random() * sum;
        for (let i = 0; i < adjustedWeights.length; i++) {
            if (rand < adjustedWeights[i]) return i;
            rand -= adjustedWeights[i];
        }
        return adjustedWeights.length - 1;
    }

    static chooseN(arr, n) {
        if (n >= arr.length) return arr.slice();

        const result = [];
        const usedIndices = new Set();

        while (result.length < n) {
            const index = Random.int(0, arr.length - 1);
            if (!usedIndices.has(index)) {
                usedIndices.add(index);
                result.push(arr[index]);
            }
        }
        return result;
    }
}