Skip to content
← Back to Community
How to make a Random number generator in python?
Profile icon
Mendelevium

Hello,
I’ve been working on a simple game, when it occurred to me:
I don’t know how to make a random number generator in python!
I’ve looked it up, but I cannot figure it out. Does anyone know how to do this?

Voters
Profile icon
AlvaroCaro
Profile icon
Mendelevium
Comments
hotnewtop
Profile icon
Pepper100

try this

import random
number = random.ranint(5, 15)

This code will generate a random number between 5 and 15. you can change these two numbers to suit you.

It will also save the selected number under the variable,"number".
you can change this by changing wheere it says number at the start of the code. you can chek the number saved by using this line of code.

print(number)

again, if instead of using number as your variable, you can change it whenever you like, and put that in the brackets of the print command

Hope this helped :)

Profile icon
slip1244
Profile icon
Mendelevium

@nithilan4 , it doesn’t work for some reason, any idea on what I could be doing wrong?