数据结构
coderljw 2024-10-13 小于 1 分钟
# 1. 随机数
Math.random
随机是均等的
const random = (range = 0.5) => {
let times = 0
const num = 1_000_000
for (let i = 0; i < num; i++) {
if (Math.random() < range) times++
}
return times / num
}
const random = () => {
const accrue: Record<string, number> = {}
const num = 1_000_000
for (let i = 0; i < num; i++) {
const num = (Math.random() * 10 + 1) | 0
accrue[num] = accrue[num] ? accrue[num] + 1 : 1
}
return accrue
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22