如何循环random.randint(0,1000)一行一行地打印所有结果?

时间:2015-02-16 22:33:34

标签: python windows batch-file

如标题中所述:

我正在尝试循环播放,以便逐行打印所有结果:

k=random.randint(0, 1000)

我正在使用批处理文件来保持打印随机数,但速度非常慢:

@echo off
cls
:start
k.py
goto start

我怎么能在python中做到这一点?

1 个答案:

答案 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)