The Python Oracle

Generate random integers between 0 and 9

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Meditation

--

Chapters
00:00 Question
00:19 Accepted answer (Score 2526)
00:32 Answer 2 (Score 734)
00:59 Answer 3 (Score 183)
01:13 Answer 4 (Score 101)
01:30 Thank you

--

Full question
https://stackoverflow.com/questions/3996...

Accepted answer links:
[random.randrange]: https://docs.python.org/3/library/random...

Answer 2 links:
[random.randint]: https://docs.python.org/3.1/library/rand...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #random #integer

#avk47



ACCEPTED ANSWER

Score 2640


Try random.randrange:

from random import randrange
print(randrange(10))



ANSWER 2

Score 827


Try random.randint:

import random
print(random.randint(0, 9))

Docs state:

random.randint(a, b)

Return a random integer N such that a <= N <= b. Alias for randrange(a, b+1).




ANSWER 3

Score 194


Try this:

from random import randrange, uniform

# randrange gives you an integral value
irand = randrange(0, 10)

# uniform gives you a floating-point value
frand = uniform(0, 10)



ANSWER 4

Score 105


from random import randint

x = [randint(0, 9) for p in range(0, 10)]

This generates 10 pseudorandom integers in range 0 to 9 inclusive.