Generate random integers between 0 and 9

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

    Choose the size of the array (in this example, I have chosen the size to be 20). And then, use the following:

    import numpy as np   
    np.random.randint(10, size=(1, 20))
    

    You can expect to see an output of the following form (different random integers will be returned each time you run it; hence you can expect the integers in the output array to differ from the example given below).

    array([[1, 6, 1, 2, 8, 6, 3, 3, 2, 5, 6, 5, 0, 9, 5, 6, 4, 5, 9, 3]])
    

提交回复
热议问题