设计一个函数def randomnum(n):,返回一个长度为n的随机整数列表,要求: 随机整数的范围:1-100 随机整数不能出现7(例如17,71),不能被7整除(例如14,28) import random def randomnum(n): # 补充函数代码
时间: 2024-03-22 17:41:14 浏览: 25
好的,下面是实现函数的代码:
```python
import random
def randomnum(n):
nums = []
while len(nums) < n:
# 随机生成1-100之间的整数
num = random.randint(1, 100)
# 判断生成的随机数是否符合要求
if '7' not in str(num) and num % 7 != 0:
nums.append(num)
return nums
# 调用函数
print(randomnum(10)) # 输出一个长度为10的随机整数列表
```
函数中,我们先定义一个空列表`nums`用来存放随机整数。在循环中,我们不断生成1-100之间的随机整数`num`,然后判断其是否符合要求,即是否包含数字7和是否能被7整除,如果符合则将其加入`nums`列表中。当列表长度达到n时,函数返回列表。
相关问题
设计一个函数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的随机数
```
相关推荐
![](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)