Appearance
生成随机数
本节目标:理解“生成随机数”的核心思路,并能把它用于实际开发或面试表达。
说明:原 OneNote 中有图片引用,但图片未包含在导出目录中;本页保留了可用的文字与代码内容。 一、随机浮点数的生成1**,生成** [ 0, 1 ) 范围内的随机数(大于等于0,小于1) (1)使用 random() 方法可以返回一个介于 0 ~ 1 之间的伪随机数(包括 0,不包括 1)。
| |
1Math.random()(2)下面是一个测试样例| ``` | |
| 1 | |
| 2 |
text
|```
**var** random = Math.random();
console.log(random);
```text
|
[](https://www.hangge.com/blog/cache/detail_1872.html#)
**2****,生成** **[ n, m )** **范围内的随机数(大于等于****n****,小于****m****)**
(1)这种最简单,因为和 **random** 的特点保持一致。只需使用如下公式即可:
|
|1
Math.random()*(m-n)+n
(2)比如下面生成 [10,15) 范围内的随机浮点数。
| | |
|---|---|
|```
1
2
3
4
5
6
```text
|```
**var** random1 = Math.random()*(15-10)+10;
**var** random2 = Math.random()*(15-10)+10;
**var** random3 = Math.random()*(15-10)+10;
console.log(random1);
console.log(random2);
console.log(random3);
```text
|
[](https://www.hangge.com/blog/cache/detail_1872.html#)
**3****,生成** **[n,m]****、****(n,m)****、****(n,m]** **范围内的随机数**
因为 **random** 的特点,要取得这几个区间内的浮点数稍微麻烦些,需要借助一些判断才能满足要求。
| | |
|---|---|
|```
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
```text
|```
//取得[n,m]范围随机数
**function** fullClose(n,m) {
**var** 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) {
**var** result = Math.random()*(m-n)+n;
**while**(result == n) {
result = Math.random()*(m-n)+n;
}
**return** result;
}
//取得(n,m]范围随机数
**function** leftOpen(n,m) {
**var** result = Math.random()*(m-n+1)+n-1;
**while**(result<n) {
result = Math.random()*(m-n+1)+n-1;
}
**return** result;
}
```text
|
**二、随机整数的生成**
要生成随机整数,我们还需要借助如下两个方法:Math.round(num):将 num 四舍五入取整
**Math.floor(num)**:将 **num** 向下取整,即返回 **num** 的整数部分。当然我们也可以使用 **parseInt()** 方法代替。
**1****,随机生成** **0****、****1** **这两个整数**
(1)下面这个方法可以随机获取 **0** 或 **1**,它们获取到的几率是比较均衡的。
|
|1
Math.round(Math.random())
(2)下面是一个测试样例
| | |
|---|---|
|```
1
2
3
4
5
6
```text
|```
**var** random1 = Math.round(Math.random());
**var** random2 = Math.round(Math.random());
**var** random3 = Math.round(Math.random());
console.log(random1);
console.log(random2);
console.log(random3);
```text
|
[](https://www.hangge.com/blog/cache/detail_1872.html#)
**2****,生成** **[ 0, n )** **范围内的随机整数(大于等于****0****,小于****n****)**
(1)下面方法生成一个 **0** 到 **n-1** 的随机整数(这 **n** 个数获取几率都是均衡的)
|
|1
Math.floor(Math.random()*n)
(2)比如下面生成几个 0 到 4 的随机整数(包括 0 和 4)。
| | |
|---|---|
|```
1
2
3
4
5
6
```text
|```
**var** random1 = Math.floor(Math.random()*5);
**var** random2 = Math.floor(Math.random()*5);
**var** random3 = Math.floor(Math.random()*5);
console.log(random1);
console.log(random2);
console.log(random3);
```text
|
[](https://www.hangge.com/blog/cache/detail_1872.html#)
**3****,生成** **[ 1, n ]** **范围内的随机整数(大于等于****1****,小于等于****n****)**
(1)下面方法生成一个 **1** 到 **n** 的随机整数(这 **n** 个数获取几率都是均衡的)
|
|1
Math.floor(Math.random()*n)+1
(2)比如下面生成几个 1 到 5 的随机整数(包括 1 和 5)。
| | |
|---|---|
|```
1
2
3
4
5
6
```text
|```
**var** random1 = Math.floor(Math.random()*5)+1;
**var** random2 = Math.floor(Math.random()*5)+1;
**var** random3 = Math.floor(Math.random()*5)+1;
console.log(random1);
console.log(random2);
console.log(random3);
```text
|
[](https://www.hangge.com/blog/cache/detail_1872.html#)
**4****,生成** **[ min, max ]** **范围内的随机整数(大于等于****min****,小于等于****max****)**
(1)下面方法生成一个最小值为 **min**,最大值为 **max** 的随机整数。
|
|1
Math.floor(Math.random()*(max-min+1))+min
(2)比如下面生成几个 5 到 10 的随机整数
| | |
|---|---|
|```
1
2
3
4
5
6
```text
|```
**var** random1 = Math.floor(Math.random()*(10-5+1))+5;
**var** random2 = Math.floor(Math.random()*(10-5+1))+5;
**var** random3 = Math.floor(Math.random()*(10-5+1))+5;
console.log(random1);
console.log(random2);
console.log(random3);
```text
|
[](https://www.hangge.com/blog/cache/detail_1872.html#)三、随机字符串的生成1**,生成指定位数的纯数字字符串**
| | |
|---|---|
|```
1
2
3
4
5
6
7
8
9
10
11
12
13
```text
|```
//生成n位数字字符串
**function** randomNum(n){
**var** res = "";
**for**(**var** 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))
```text
|
[](https://www.hangge.com/blog/cache/detail_1872.html#)2**,生成指定位数的数字字母混合的字符串**
| | |
|---|---|
|```
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
```text
|```
//生成n位数字字母混合字符串
**function** generateMixed(n) {
**var** 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'];
**var** res = "";
**for**(**var** i = 0; i < n ; i++) {
**var** id = Math.floor(Math.random()*36);
res += chars[id];
}
**return** res;
}
//测试
console.log(generateMixed(3))
console.log(generateMixed(5))
console.log(generateMixed(7))
```text
|
[](https://www.hangge.com/blog/cache/detail_1872.html#)原文出自:www.hangge.com 转载请保留原文链接:https://www.hangge.com/blog/cache/detail_1872.html
\> 来自https://www.hangge.com/blog/cache/detail_1872.html
[](https://www.hangge.com/blog/cache/detail_1872.html#) [](https://www.hangge.com/blog/cache/detail_1872.html#)
[](https://www.hangge.com/blog/cache/detail_1872.html#)
[](https://www.hangge.com/blog/cache/detail_1872.html#)
[](https://www.hangge.com/blog/cache/detail_1872.html#)
[](https://www.hangge.com/blog/cache/detail_1872.html#)
[](https://www.hangge.com/blog/cache/detail_1872.html#)
[](https://www.hangge.com/blog/cache/detail_1872.html#)
\> 来自https://www.hangge.com/blog/cache/detail_1872.html
