Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

公平打乱数字 #11

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open

Conversation

wyp20040208123
Copy link

static RandomSort(list) {
// 检查输入是否是数组
if (!Array.isArray(list)) {
throw new Error('Input must be an array');
}

// Fisher-Yates 洗牌算法
for (let i = list.length - 1; i > 0; i--) {
    // 随机选择一个索引 j,范围为 0 到 i
    const j = Math.floor(Math.random() * (i + 1));
    
    // 交换 list[i] 和 list[j]
    [list[i], list[j]] = [list[j], list[i]];
}

return list;  // 返回打乱后的数组

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants