随机生成整数随机数python
时间: 2023-08-18 21:11:27 浏览: 94
在Python中,可以使用random模块来生成随机整数和浮点数。通过使用random.randint(n, m)可以生成一个n到m之间的随机整数,而random.random()可以生成一个0到1之间的随机浮点数。如果想生成一个范围内的不重复的随机整数列表,可以使用random.sample(range(a, b), n),其中a和b是范围的起始和结束值,n是要生成的随机整数的个数。下面是一个生成10个1到100之间不重复随机整数的例子:
import random
numbers = random.sample(range(1, 100), 10)
print(numbers)
输出结果可能为:
<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [【Python 基础教程】Python生成随机数](https://blog.csdn.net/apple_51931783/article/details/123144689)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [python生成随机数](https://blog.csdn.net/m0_53653974/article/details/125753330)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [Python生成随机整数(1个、n个、指定步长、不重复)](https://blog.csdn.net/xucanlax/article/details/127685058)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]
阅读全文