Generate random integers between 0 and 9

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

    This is more of a mathematical approach but it works 100% of the time:

    Let's say you want to use random.random() function to generate a number between a and b. To achieve this, just do the following:

    num = (b-a)*random.random() + a;

    Of course, you can generate more numbers.

提交回复
热议问题