如标题中所述:
我正在尝试循环播放,以便逐行打印所有结果:
k=random.randint(0, 1000)
我正在使用批处理文件来保持打印随机数,但速度非常慢:
@echo off
cls
:start
k.py
goto start
我怎么能在python中做到这一点?
答案 0 :(得分:2)
import random
# prints an endless list of random numbers
while True:
num = random.randint(0, 1000)
print(num)
其他替代方案:
# print 1000 random numbers
for i in range(1000):
num = random.randint(0, 1000)
print(num)
或
# print the numbers 0..1000 in random order
nums = list(range(1001))
random.shuffle(nums) # shuffle list in-place
for num in nums:
print(num)