This works almost fine but the number starts with 0 sometimes:
import random numbers = random.sample(range(10), 4) print(\'\'.join(map(str, numbers)))
rejection sampling method. Create a 4 digit random combination from 10 digits and resample if it doesn't match the criteria.
r4=0 while r4 < 1000: r4=int(''.join(map(str,random.sample(range(10),4))))
noticed that this is essentially the same as @Austin Haskings's answer