Really concise explanation of random integers in Python

I wanted to use Python to generate some random integers, and a few minutes of Googling was not very helpful. I figured it out in the end, but I feel like something this basic should be easy.

First you need to import the random module, so:

import random

Then, to get a random integer from 1 to x, use

random.randrange(x)

It’s as easy as that.

(OK, OK, sorry. I know this probably isn’t of general interest, and it’s probably of minimal help, but I was just irritated that it wasn’t really easy to find online.)

Share/Save/Bookmark

Leave a Comment