# js 生成随机数
# 1、随机浮点数生成
Math.random();
1
# 1.2 生成 [ n, m ] 范围内的随机数(大于等于 n,小于 m)
Math.random() * (m - n) + n;
1
# 1.3 生成 [ n, m ]、[ n, m ]、[ n, m ] 范围内的随机数
//取得[n,m]范围随机数
function fullClose(n, m) {
let result = Math.random() * (m + 1 - n) + n;
while (result > m) {
result = Math.random() * (m + 1 - n) + n;
}
return result;
}
//取得(n,m)范围随机数
function fullOpen(n, m) {
let result = Math.random() * (m - n) + n;
while (result == n) {
result = Math.random() * (m - n) + n;
}
return result;
}
//取得(n,m]范围随机数
function leftOpen(n, m) {
let result = Math.random() * (m - n + 1) + n - 1;
while (result < n) {
result = Math.random() * (m - n + 1) + n - 1;
}
return result;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 2、随机整数生成
# 2.1 随机生成 0、1 这两个整数
Math.round(Math.random());
1
# 2.2 生成 [ 0, n ] 范围内的随机整数(大于等于 0,小于 n)
Math.floor(Math.random() * n);
1
# 2.3 生成 [ 1, n ] 范围内的随机整数(大于等于 1,小于等于 n)
Math.floor(Math.random() * n) + 1;
1
# 2.4 生成 [ min, max ] 范围内的随机整数(大于等于 min,小于等于 max
Math.floor(Math.random() * (max - min + 1)) + min;
1
# 3、随机字符串生成
# 3.1 生成指定位数的纯数字字符串
//生成n位数字字符串
function randomNum(n) {
let res = '';
for (let i = 0; i < n; i++) {
res += Math.floor(Math.random() * 10);
}
return res;
}
//测试
console.log(randomNum(3));
console.log(randomNum(5));
console.log(randomNum(7));
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# 3.2 生成指定位数的数字字母混合的字符串
//生成n位数字字母混合字符串
function generateMixed(n) {
let chars = [
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
'Y',
'Z',
];
let res = '';
for (let i = 0; i < n; i++) {
let id = Math.floor(Math.random() * 36);
res += chars[id];
}
return res;
}
//测试
console.log(generateMixed(3));
console.log(generateMixed(5));
console.log(generateMixed(7));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 3.3 生成随机密码(包含特殊字符)
// Random user password
export const randomPassword = (length) => {
if (length < 6) {
length = 6;
} else if (length > 16) {
length = 16;
}
let passwordArray = [
'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'abcdefghijklmnopqrstuvwxyz',
'1234567890',
'!@#$%&*()',
];
const password = [];
let n = 0;
for (let i = 0; i < length; i++) {
// If password length less than 9, all value random
if (password.length < length - 4) {
// Get random passwordArray index
let arrayRandom = Math.floor(Math.random() * 4);
// Get password array value
let passwordItem = passwordArray[arrayRandom];
// Get password array value random index
// Get random real value
let item = passwordItem[Math.floor(Math.random() * passwordItem.length)];
password.push(item);
} else {
// If password large then 9, lastest 4 password will push in according to the random password index
// Get the array values sequentially
let newItem = passwordArray[n];
let lastItem = newItem[Math.floor(Math.random() * newItem.length)];
// Get array splice index
let spliceIndex = Math.floor(Math.random() * password.length);
password.splice(spliceIndex, 0, lastItem);
n++;
}
}
return password.join('');
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39