Ask coding questions

← Back to all posts
How to make a Random number generator in python?
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
AlvaroCaro
Mendelevium
Comments
hotnewtop
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 :)

slip1244
Mendelevium

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