Generate random integers between 0 and 9

前端 未结 19 1734
无人共我
无人共我 2020-11-22 15:37

How can I generate random integers between 0 and 9 (inclusive) in Python?

For example, 0, 1, 2, 3, 4

19条回答
  •  渐次进展
    2020-11-22 16:28

    Best way is to use import Random function

    import random
    print(random.sample(range(10), 10))
    

    or without any library import:

    n={} 
    for i in range(10):
        n[i]=i
    
    for p in range(10):
        print(n.popitem()[1])
    

    here the popitems removes and returns an arbitrary value from the dictionary n.

提交回复
热议问题