How can I generate random integers between 0 and 9 (inclusive) in Python?
For example, 0
, 1
, 2
, 3
, 4
, 5
, 6
, 7
, 8
, 9
Random rand = new Random(); int x = rand. nextInt(10); x will be between 0-9 inclusive. So, given the following array of 25 items, the code to generate a random number between 0 (the base of the array) and array.
Use randint() Generate random integer Use a random. randint() function to get a random integer number from the inclusive range. For example, random. randint(0, 10) will return a random number from [0, 1, 2, 3, 4, 5, 6, 7, 8 ,9, 10].
With the help of the seed value, srand() sets the stage for the generation of pseudo-random numbers by the rand() function. int random = rand(); And Voila! We have achieved the simple task of generating a random number.
Try:
from random import randrange print(randrange(10))
Docs: https://docs.python.org/3/library/random.html#random.randrange
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With