设计一个函数def randomnum(n):,返回一个长度为n的随机整数列表,要求: 随机整数的范围:1-100 随机整数不能出现7(例如17,71),不能被7整除(例如14,28) import random def randomnum(n): # 补充函数代码
时间: 2024-03-27 13:35:45 浏览: 11
可以通过循环生成随机整数,并判断是否符合要求,如果符合要求则添加到列表中,直到列表长度达到n为止。具体实现可以参考如下代码:
```python
import random
def randomnum(n):
nums = []
while len(nums) < n:
num = random.randint(1, 100) # 生成1-100之间的随机整数
if '7' not in str(num) and num % 7 != 0: # 判断是否符合要求
nums.append(num)
return nums
```
这样调用函数`randomnum(10)`就可以返回一个长度为10的符合要求的随机整数列表了。
相关问题
设计一个函数def randomnum(n):,返回一个长度为n的随机整数列表,要求: 随机整数的范围:1-100 随机整数不能出现7(例如17,71),不能被7整除(例如14,28)
以下是Python3的代码实现:
```python
import random
def randomnum(n):
"""返回一个长度为n的随机整数列表"""
nums = [] # 存储随机整数的列表
while len(nums) < n:
num = random.randint(1, 100) # 生成1-100之间的随机整数
if '7' not in str(num) and num % 7 != 0: # 判断是否符合要求
nums.append(num)
return nums
```
使用方法:
调用 `randomnum(n)` 函数,其中 `n` 为要生成的随机整数列表的长度。例如,要生成长度为 10 的随机整数列表,可以调用 `randomnum(10)` 函数。
Java中随机生成一个0到1的整数
Java中无法生成0到1的整数。因为0到1之间的整数只有0,而0不是一个区间,因此无法生成。如果需要生成0或1的随机数,可以使用以下代码:
```
int randomNum = (int)(Math.random() * 2); //生成0或1的随机数
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.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)
![](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)