How to generate a random 4 digit number not starting with 0 and having unique digits?

后端 未结 12 1976
没有蜡笔的小新
没有蜡笔的小新 2020-12-28 12:48

This works almost fine but the number starts with 0 sometimes:

import random
numbers = random.sample(range(10), 4)
print(\'\'.join(map(str, numbers)))
         


        
12条回答
  •  我在风中等你
    2020-12-28 13:08

    This will allow zeros after the first digit -

    numbers = random.sample(range(1,10),1) + random.sample(range(10),3)
    

提交回复
热议问题