Skip to main content
2 of 4
added 3 characters in body; edited title

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

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

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

I've found a lot of examples but none of them guarantee that the sequence won't start with 0.

Menon A.
  • 580
  • 1
  • 5
  • 13