IG获胜抽奖-基于种子可复现的洗牌算法

519 阅读1分钟

今天是IG打SKT,如果IG赢了抽3杯瑞幸。下面是抽奖算法,可复现,以保证结果公开透明。

种子seed为10/15上午发沸点时间(即开奖时间),再加上比分总和。例如,seed=20251015083001+4

/**
 * 抽奖算法
 * @param {number} total 总人数
 * @param {number} winners 中奖人数
 * @param {number} [seed] 随机种子,用于复现结果
 * @returns {number[]} 中奖人的序号数组(从1开始)
 */
function lottery(total, winners, seed) {
    if (typeof total !== 'number' || typeof winners !== 'number' || total < 1 || winners < 1 || winners > total) {
        throw new Error('error parameter');
    }
    const randomSeed = seed !== undefined ? seed : Date.now();
    function mulberry32(seed) {
        return function() {
            seed += 0x6D2B79F5;
            let t = seed;
            t = Math.imul(t ^ t >>> 15, t | 1);
            t ^= t + Math.imul(t ^ t >>> 7, t | 61);
            return ((t ^ t >>> 14) >>> 0) / 4294967296;
        }
    }
    const random = mulberry32(randomSeed);
    const participants = Array.from({length: total}, (_, i) => i + 1);
    // Fisher-Yates
    for (let i = participants.length - 1; i > 0; i--) {
        const j = Math.floor(random() * (i + 1));
        [participants[i], participants[j]] = [participants[j], participants[i]];
    }
    const result = participants.slice(0, winners);
    result.sort((a, b) => a - b);
    return result;
}

测试部分

const seed = 20251015083001+4;
const result = lottery(100, 3, seed);
console.log(`(种子 ${seed})中奖号码:`, result);