Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate random integers between 0 and 9

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

like image 931
aneuryzm Avatar asked Oct 22 '10 12:10

aneuryzm


People also ask

How do you generate a random number from 0 to 9 in Java?

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.

How do you generate a random number from 0-9 in Python?

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].

How do you generate a random number between 0 and 9 in C++?

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.


1 Answers

Try:

from random import randrange print(randrange(10)) 

Docs: https://docs.python.org/3/library/random.html#random.randrange

like image 83
kovshenin Avatar answered Sep 19 '22 12:09

kovshenin