mirror of
https://github.com/justhtmls/html-tools.git
synced 2026-09-01 15:32:50 +08:00
fix: 修复随机数生成器和随机抽取器的输出问题
- 随机数生成器: 添加输入验证、防止无限循环、范围检查 - 随机抽取器: 修复换行符转义问题、添加抽取数量验证 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -169,13 +169,36 @@
|
||||
const max = parseInt(document.getElementById('random-max').value, 10);
|
||||
const count = parseInt(document.getElementById('random-count').value, 10) || 1;
|
||||
const unique = document.getElementById('random-unique').checked;
|
||||
|
||||
// 验证输入
|
||||
if (isNaN(min) || isNaN(max) || isNaN(count)) {
|
||||
alert('请输入有效的数字');
|
||||
return;
|
||||
}
|
||||
if (min > max) {
|
||||
alert('最小值不能大于最大值');
|
||||
return;
|
||||
}
|
||||
if (count < 1) {
|
||||
alert('数量必须大于0');
|
||||
return;
|
||||
}
|
||||
if (count > 1000) {
|
||||
alert('单次最多生成1000个随机数');
|
||||
return;
|
||||
}
|
||||
if (unique && (max - min + 1) < count) {
|
||||
alert(`范围 ${min}-${max} 内只有 ${max - min + 1} 个数字,无法生成 ${count} 个不重复的随机数`);
|
||||
return;
|
||||
}
|
||||
|
||||
const results = [];
|
||||
const used = new Set();
|
||||
while (results.length < count) {
|
||||
let attempts = 0;
|
||||
const maxAttempts = count * 100; // 防止无限循环
|
||||
|
||||
while (results.length < count && attempts < maxAttempts) {
|
||||
attempts++;
|
||||
const num = Math.floor(Math.random() * (max - min + 1)) + min;
|
||||
if (unique) {
|
||||
if (used.has(num)) continue;
|
||||
@@ -183,6 +206,12 @@
|
||||
}
|
||||
results.push(num);
|
||||
}
|
||||
|
||||
if (results.length < count) {
|
||||
alert('生成随机数失败,请尝试增大范围或减少数量');
|
||||
return;
|
||||
}
|
||||
|
||||
document.getElementById('random-output').value = results.join(', ');
|
||||
}
|
||||
|
||||
|
||||
@@ -163,28 +163,38 @@ Charlie"></textarea>
|
||||
|
||||
<script>
|
||||
function pickRandomItems() {
|
||||
const items = document.getElementById('picker-items').value.split(/
|
||||
?
|
||||
/).filter(Boolean);
|
||||
const count = parseInt(document.getElementById('picker-count').value, 10) || 1;
|
||||
const unique = document.getElementById('picker-unique').checked;
|
||||
if (items.length === 0) {
|
||||
alert('请先输入列表');
|
||||
return;
|
||||
}
|
||||
const pool = [...items];
|
||||
const result = [];
|
||||
for (let i = 0; i < count && pool.length > 0; i++) {
|
||||
const index = Math.floor(Math.random() * pool.length);
|
||||
result.push(pool[index]);
|
||||
if (unique) pool.splice(index, 1);
|
||||
}
|
||||
document.getElementById('picker-output').value = result.join('
|
||||
const items = document.getElementById('picker-items').value.split(/\r?\n/).filter(Boolean);
|
||||
const count = parseInt(document.getElementById('picker-count').value, 10) || 1;
|
||||
const unique = document.getElementById('picker-unique').checked;
|
||||
|
||||
if (items.length === 0) {
|
||||
alert('请先输入列表');
|
||||
return;
|
||||
}
|
||||
if (count < 1) {
|
||||
alert('抽取数量必须大于0');
|
||||
return;
|
||||
}
|
||||
if (unique && count > items.length) {
|
||||
alert(`列表中只有 ${items.length} 项,无法抽取 ${count} 个不重复项目`);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
function clearOutput() {
|
||||
const pool = [...items];
|
||||
const result = [];
|
||||
|
||||
for (let i = 0; i < count && pool.length > 0; i++) {
|
||||
const index = Math.floor(Math.random() * pool.length);
|
||||
result.push(pool[index]);
|
||||
if (unique) pool.splice(index, 1);
|
||||
}
|
||||
|
||||
document.getElementById('picker-output').value = result.join('\n');
|
||||
}
|
||||
|
||||
function clearOutput() {
|
||||
document.getElementById('picker-output').value = '';
|
||||
}
|
||||
</script>
|
||||
|
||||
<script src="/assets/clicks.js" defer></script>
|
||||
|
||||
Reference in New Issue
Block a user