使用Python在文本文件中连续生成随机坐标



是否有一种方法可以使用Python连续将随机坐标添加到文本文件中?(更新(

import random
import threading
#Open a file named numbersmake.txt.
outfile = open('new.txt', 'w')
def coordinate():
    threading.Timer(0.0000000000001, coordinate).start ()
    x = random.randint(0,10000)
    y = random.randint(0,10000)
    outfile.write("{},{}n".format(x, y))
coordinate()
#Close the file.
outfile.close()
print('The data is now the the new.txt file')

基于您的代码,您似乎只是试图在一枪和退出中生成12,000个随机坐标。如果是这种情况,为什么要线程?仅当您打算在线程处理坐标时使用程序做其他事情时,您才需要线程。

也许如果您的坐标生成是由某些外部,不可预测的事件触发的,那可能是有道理的。否则,如果您真的只是尽快生成有限的坐标集,我认为您的范围解决方案很好。

有关您真正要做的事情的更多详细信息将有助于制定更好的解决方案。

最新更新