js生成10个1到100随机整数数组
时间: 2023-05-28 17:07:29 浏览: 371
以下是一种可能的方法,使用 Math.random() 函数生成随机数,并使用 Math.floor() 函数将其舍入为整数:
```javascript
// 生成10个随机整数数组
for (var i = 0; i < 10; i++) {
var arr = [];
for (var j = 0; j < 10; j++) {
arr.push(Math.floor(Math.random() * 100) + 1);
}
console.log(arr);
}
```
这将输出10个包含10个随机整数的数组,每个整数的范围在1到100之间。
相关问题
js 生成随机100到1000内没有位数为0的整数
可以先通过循环生成 100 到 1000 之间的整数,再判断该整数是否包含数字 0,如果不包含则输出该整数,否则继续循环生成下一个整数。
以下是代码实现:
```
function generateRandomInt() {
let randomInt = 0;
do {
randomInt = Math.floor(Math.random() * (1000 - 100) + 100); // 生成 100 到 1000 之间的整数
} while (randomInt.toString().includes('0')); // 判断是否包含数字 0
return randomInt;
}
const randomInt = generateRandomInt();
console.log(randomInt);
```
每次执行时,会随机生成一个 100 到 1000 之间没有包含数字 0 的整数,并打印输出。
js 生成随机1到33中6个数
以下是一种实现方式:
```javascript
function generateLotteryNumbers() {
var numbers = [];
while (numbers.length < 6) {
var random = Math.floor(Math.random() * 33) + 1;
if (numbers.indexOf(random) === -1) {
numbers.push(random);
}
}
return numbers;
}
console.log(generateLotteryNumbers()); // 输出类似 [5, 12, 25, 27, 30, 33] 的数组
```
这里使用了 `Math.random()` 函数来生成一个介于 0 和 1 之间的随机数,然后乘以 33,再加上 1,就可以得到一个介于 1 和 33 之间的随机整数。通过一个 `while` 循环,不断生成随机数,直到生成了 6 个不重复的数为止。最后将这 6 个数存储在一个数组中并返回。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)