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

后端 未结 12 1951
没有蜡笔的小新
没有蜡笔的小新 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:11

    [Fixed] Shift all four digits on one position is not right. Swap leading zero with fixed position is not right too. But random swap of the leading zero with any of nine positions is correct and gives equal probability:

    """ Solution: randomly shuffle all numbers. If 0 is on the 0th position,
                  randomly swap it with any of nine positions in the list.
    
      Proof
        Lets count probability for 0 to be in position 7. It is equal to probability 1/10 
      after shuffle, plus probability to be randomly swapped in the 7th position if
      0 come to be on the 0th position: (1/10 * 1/9). In total: (1/10 + 1/10 * 1/9).
        Lets count probability for 3 to be in position 7. It is equal to probability 1/10
      after shuffle, minus probability to be randomly swapped in the 0th position (1/9)
      if 0 come to be on the 0th position (1/10) and if 3 come to be on the 7th position
      when 0 is on the 0th position (1/9). In total: (1/10 - 1/9 * 1/10 * 1/9).
        Total probability of all numbers [0-9] in position 7 is:
      9 * (1/10 - 1/9 * 1/10 * 1/9) + (1/10 + 1/10 * 1/9) = 1
        Continue to prove in the same way that total probability is equal to
      1 for all other positions.
        End of proof. """
    
    import random
    l = [0,1,2,3,4,5,6,7,8,9]
    random.shuffle(l)
    if l[0] == 0:
        pos = random.choice(range(1, len(l)))
        l[0], l[pos] = l[pos], l[0]
    print(''.join(map(str, l[0:4])))
    

提交回复
热议问题