Skip to content

Commit af4c0e2

Browse files
chao5goclaude
andcommitted
fix: 修复随机数生成器和随机抽取器的输出问题
- 随机数生成器: 添加输入验证、防止无限循环、范围检查 - 随机抽取器: 修复换行符转义问题、添加抽取数量验证 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 58803bf commit af4c0e2

2 files changed

Lines changed: 59 additions & 20 deletions

File tree

tools/random-number/app.html

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,20 +169,49 @@ <h1>随机数生成器</h1>
169169
const max = parseInt(document.getElementById('random-max').value, 10);
170170
const count = parseInt(document.getElementById('random-count').value, 10) || 1;
171171
const unique = document.getElementById('random-unique').checked;
172+
173+
// 验证输入
174+
if (isNaN(min) || isNaN(max) || isNaN(count)) {
175+
alert('请输入有效的数字');
176+
return;
177+
}
172178
if (min > max) {
173179
alert('最小值不能大于最大值');
174180
return;
175181
}
182+
if (count < 1) {
183+
alert('数量必须大于0');
184+
return;
185+
}
186+
if (count > 1000) {
187+
alert('单次最多生成1000个随机数');
188+
return;
189+
}
190+
if (unique && (max - min + 1) < count) {
191+
alert(`范围 ${min}-${max} 内只有 ${max - min + 1} 个数字,无法生成 ${count} 个不重复的随机数`);
192+
return;
193+
}
194+
176195
const results = [];
177196
const used = new Set();
178-
while (results.length < count) {
197+
let attempts = 0;
198+
const maxAttempts = count * 100; // 防止无限循环
199+
200+
while (results.length < count && attempts < maxAttempts) {
201+
attempts++;
179202
const num = Math.floor(Math.random() * (max - min + 1)) + min;
180203
if (unique) {
181204
if (used.has(num)) continue;
182205
used.add(num);
183206
}
184207
results.push(num);
185208
}
209+
210+
if (results.length < count) {
211+
alert('生成随机数失败,请尝试增大范围或减少数量');
212+
return;
213+
}
214+
186215
document.getElementById('random-output').value = results.join(', ');
187216
}
188217

tools/random-picker/app.html

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -163,28 +163,38 @@ <h1>随机抽取器</h1>
163163

164164
<script>
165165
function pickRandomItems() {
166-
const items = document.getElementById('picker-items').value.split(/?
167-
/).filter(Boolean);
168-
const count = parseInt(document.getElementById('picker-count').value, 10) || 1;
169-
const unique = document.getElementById('picker-unique').checked;
170-
if (items.length === 0) {
171-
alert('请先输入列表');
172-
return;
173-
}
174-
const pool = [...items];
175-
const result = [];
176-
for (let i = 0; i < count && pool.length > 0; i++) {
177-
const index = Math.floor(Math.random() * pool.length);
178-
result.push(pool[index]);
179-
if (unique) pool.splice(index, 1);
180-
}
181-
document.getElementById('picker-output').value = result.join('
182-
');
166+
const items = document.getElementById('picker-items').value.split(/\r?\n/).filter(Boolean);
167+
const count = parseInt(document.getElementById('picker-count').value, 10) || 1;
168+
const unique = document.getElementById('picker-unique').checked;
169+
170+
if (items.length === 0) {
171+
alert('请先输入列表');
172+
return;
173+
}
174+
if (count < 1) {
175+
alert('抽取数量必须大于0');
176+
return;
183177
}
178+
if (unique && count > items.length) {
179+
alert(`列表中只有 ${items.length} 项,无法抽取 ${count} 个不重复项目`);
180+
return;
181+
}
182+
183+
const pool = [...items];
184+
const result = [];
184185

185-
function clearOutput() {
186-
document.getElementById('picker-output').value = '';
186+
for (let i = 0; i < count && pool.length > 0; i++) {
187+
const index = Math.floor(Math.random() * pool.length);
188+
result.push(pool[index]);
189+
if (unique) pool.splice(index, 1);
187190
}
191+
192+
document.getElementById('picker-output').value = result.join('\n');
193+
}
194+
195+
function clearOutput() {
196+
document.getElementById('picker-output').value = '';
197+
}
188198
</script>
189199

190200
<script src="/assets/clicks.js" defer></script>

0 commit comments

Comments
 (0)