【发布时间】:2022-01-05 03:37:21
【问题描述】:
我正在尝试编写一种方法,在给定 Python 中的宽度和高度的情况下,在二维空间中生成并返回 n 个随机点我编写了一个算法,但我想在系统中接收浮点数。 我的代码是:
import random
npoints = int(input("Type the npoints:"))
width = int(input("Enter the Width you want:"))
height = int (input("Enter the Height you want:"))
allpoints = [(a,b) for a in range(width) for b in range(height)]
sample = random.sample(allpoints, npoints)
print(sample)
Output is:
Type the npoints:4
Enter the Width you want:10
Enter the Height you want:8
[(8, 7), (3, 3), (7, 7), (9, 0)]
如何将它们打印为浮点数。例如:(8.75 , 6.31)
非常感谢您的帮助。
【问题讨论】:
标签: python list for-loop multidimensional-array floating-point