随机数生成
随机数生成工具 🎰
Section titled “随机数生成工具 🎰”强大的随机数生成工具,支持数字、密码、字符串等多种随机生成需求。
- 🔢 随机数字 - 生成指定范围的随机数
- 🔐 随机密码 - 生成高强度安全密码
- 📝 随机字符串 - 生成随机字符组合
- 🎲 骰子模拟 - 模拟掷骰子
- 📋 批量生成 - 一次生成多个结果
-
设置范围
- 最小值:1
- 最大值:100
-
设置数量
- 生成个数:5
- 是否允许重复:否
-
点击生成
- 结果:23, 56, 8, 91, 34
1. 随机数字
Section titled “1. 随机数字”生成指定范围内的整数或小数:
范围: 1 - 100数量: 10结果: 23, 56, 8, 91, 34, 67, 12, 89, 45, 3范围: 0 - 1精度: 4 位小数数量: 5结果: 0.2341, 0.8912, 0.0567, 0.4456, 0.7823确保生成的数字不重复:
范围: 1 - 10数量: 10结果: 3, 7, 1, 9, 4, 2, 8, 5, 10, 6(每个数字只出现一次)2. 随机密码生成
Section titled “2. 随机密码生成”生成安全的高强度密码:
- 长度 - 8-128 位
- 数字 - 0-9
- 小写字母 - a-z
- 大写字母 - A-Z
- 特殊符号 - !@#$%^&*
- 排除相似字符 - 排除 i, l, 1, L, o, 0, O
| 强度 | 要求 | 示例 |
|---|---|---|
| 弱 | 仅数字 | 123456 |
| 中 | 数字+字母 | aB3dE5 |
| 强 | 数字+大小写+符号 | K9#mP2$v |
| 极强 | 16位以上混合 | X7#kM9$pL2@vN4!q |
长度: 16包含: 数字 + 大小写字母 + 特殊符号结果: K9#mP2$vL5@nQ8!x3. 随机字符串
Section titled “3. 随机字符串”生成指定格式的随机字符串:
UUID/GUID
Section titled “UUID/GUID”标准 UUID: 550e8400-e29b-41d4-a716-446655440000短 UUID: 550e8400e29b41d4a716446655440000长度: 10结果: kdjfHsnwPz随机十六进制
Section titled “随机十六进制”长度: 16结果: 3f2a8b9c1d4e5f67自定义字符集
Section titled “自定义字符集”字符集: ABCDEF123456长度: 8结果: A3F2B6C14. 骰子模拟
Section titled “4. 骰子模拟”模拟掷骰子:
面数: 6结果: 4骰子数: 3面数: 6结果: [2, 5, 3]总和: 10- D4 - 四面骰
- D6 - 六面骰(标准)
- D8 - 八面骰
- D10 - 十面骰
- D12 - 十二面骰
- D20 - 二十面骰
- D100 - 百分骰
5. 批量生成
Section titled “5. 批量生成”一次生成多个结果:
类型: 随机数范围: 1-100数量: 100导出: CSV 文件实际应用场景
Section titled “实际应用场景”场景 1:抽奖编号
Section titled “场景 1:抽奖编号”生成不重复的抽奖号码:
范围: 1000 - 9999数量: 100不重复: 是用途: 给参与者分配唯一编号场景 2:测试数据
Section titled “场景 2:测试数据”生成测试用的随机数据:
// 生成测试用户IDconst userId = 'USER' + Math.floor(Math.random() * 10000).toString().padStart(4, '0');// USER0523
// 生成随机金额const amount = (Math.random() * 1000).toFixed(2);// 523.47场景 3:密码生成
Section titled “场景 3:密码生成”为用户生成临时密码:
长度: 12规则: 大小写 + 数字 + 符号结果: X7#kM9$pL2@v场景 4:游戏随机
Section titled “场景 4:游戏随机”游戏开发中的随机应用:
// 随机掉落const dropRate = 0.15; // 15% 掉落率const dropped = Math.random() < dropRate;
// 随机伤害const baseDamage = 100;const variance = 0.2; // ±20%const damage = baseDamage * (1 + (Math.random() - 0.5) * 2 * variance);// 80 - 120 之间的随机伤害场景 5:数据采样
Section titled “场景 5:数据采样”从大量数据中随机采样:
// 从 10000 条记录中随机选 100 条const allData = [...Array(10000)].map((_, i) => i);const sample = [];
for (let i = 0; i < 100; i++) { const index = Math.floor(Math.random() * allData.length); sample.push(allData.splice(index, 1)[0]);}使用种子生成可重复的随机序列:
// 使用相同种子得到相同结果function seededRandom(seed) { const x = Math.sin(seed++) * 10000; return x - Math.floor(x);}
// 种子 12345seededRandom(12345); // 0.2345seededRandom(12345); // 0.2345 (相同)支持多种概率分布:
// Box-Muller 变换function normalRandom(mean, stdDev) { const u1 = Math.random(); const u2 = Math.random(); const z0 = Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2); return z0 * stdDev + mean;}
// 生成均值为 50,标准差为 10 的随机数normalRandom(50, 10); // 约 95% 的值在 30-70 之间const items = [ { name: '普通', weight: 70 }, { name: '稀有', weight: 20 }, { name: '史诗', weight: 9 }, { name: '传说', weight: 1 }];
function weightedRandom(items) { const total = items.reduce((sum, item) => sum + item.weight, 0); let random = Math.random() * total;
for (const item of items) { random -= item.weight; if (random <= 0) return item; }}Q: 生成的随机数真的随机吗?
Section titled “Q: 生成的随机数真的随机吗?”A: 使用伪随机数生成器,对于一般用途足够随机。如需加密安全级别的随机数,请使用 crypto.getRandomValues()。
Q: 可以生成指定格式的随机数吗?
Section titled “Q: 可以生成指定格式的随机数吗?”A: 可以!支持自定义格式,如手机号、身份证号、银行卡号等格式。
Q: 批量生成有数量限制吗?
Section titled “Q: 批量生成有数量限制吗?”A: 浏览器端建议一次不超过 10000 个,大量生成建议使用服务端工具。
Q: 随机密码安全吗?
Section titled “Q: 随机密码安全吗?”A: 使用加密安全的随机数生成,生成的密码强度高,适合日常使用。