Skip to content
← Back to Community
How do you generate random numbers in Python?
Profile icon
BOIBOI1

i just want to know...............................

Voters
Profile icon
KnightKiller453
Profile icon
KhanhVu15
Profile icon
jac1187
Profile icon
vinceSilv
Profile icon
CateMeardon
Profile icon
SagaciousPan
Profile icon
BOIBOI1
Comments
hotnewtop
Profile icon
T11tu5

These two ways are my favourite:
from random import *
randint (1, 10)
and
import random
random.randint (1, 10)

Profile icon
christiant

The easiest method to do this is:

import random random.randint(1, 10)

This simply chooses a random number between 1 and 10. You can change the two numbers to whatever you want just remember that the first number is the minimum and the second number is the maximum.

I hope this helped.

Profile icon
SagaciousPan

@BOIBOI1
The below code will print a random number between 1 and 100 ten times.

# import the 'random' library and it's commands import random # do this 10 times for x in range(10): #print a random number between 1 and 100 print (random.randint(1,101))

Test at:
https://repl.it/@SagaciousPan/Random-Number-Python

Hope I could help!

Please mark this as answered if your problem has been solved.

Profile icon
SixBeeps

https://docs.python.org/3/library/random.html
TL;DR, you can import the random library, and from there, call random.randrange(start, stop, step) to get a random number

Profile icon
mwilki7