首页 > 解决方案 > 如何在 Python 中为 boucle 做随机数?

问题描述

有没有办法在 Python 中随机化 for boucle?

像这样的东西:

for i in random(range(0,5)): #This is an example and not working at all

    print(i)

-> 并且结果应该是例如:

4

2

3

1

0

标签: python

解决方案


这有效:

import random
items = [x for x in range(0,5)] # create a list with items
random.shuffle(items) # shuffle it (in-place)
for i in items: # iterate over
    print(i) # print

random参阅相应的文档


推荐阅读