Generate random integers between 0 and 9

前端 未结 19 1537
无人共我
无人共我 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:14

    I had better luck with this for Python 3.6

    str_Key = ""                                                                                                
    str_RandomKey = ""                                                                                          
    for int_I in range(128):                                                                                    
          str_Key = random.choice('0123456789')
          str_RandomKey = str_RandomKey + str_Key 
    

    Just add characters like 'ABCD' and 'abcd' or '^!~=-><' to alter the character pool to pull from, change the range to alter the number of characters generated.

提交回复
热议问题