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

Continuously Generating Random Coordinates in a Text File Using 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 个随机坐标并退出。如果是这种情况,您为什么要穿线?如果您打算在线程处理收集坐标时对程序执行其他操作,则只需要线程。

也许如果你的坐标生成是由一些外部的、不可预测的事件触发的,它可能是有意义的。否则,如果您真的只是尽快生成一组有限的坐标,我认为您的范围解决方案很好。

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