The Python Oracle

Generate random integers between 0 and 9

--------------------------------------------------
Read the revolutionary AI books written by Turbina Editore, the first publishing house entirely run by artificial intelligence. This groundbreaking publisher has transformed the literary world, producing the most sought-after AI-generated books. Start reading today at:
https://turbinaeditore.com/en/
--------------------------------------------------

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT


Music by Eric Matyas
https://www.soundimage.org
Track title: Techno Bleepage Open

--

Chapters
00:00 Generate Random Integers Between 0 And 9
00:15 Answer 1 Score 194
00:26 Accepted Answer Score 2640
00:37 Answer 3 Score 827
00:57 Answer 4 Score 105
01:08 Thank you

--

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

--

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.