liveBook · Manning

Randint

As explained in Python Workout: 50 ten-minute exercises

randint is a function that returns a pseudo-random integer within an inclusive lower and upper bound.

Use randint when a program needs a random integer between two bounds. Its upper bound is inclusive, so random.randint(10, 30) can return 10 through 30, including both endpoints. section 1.2 section 1.3

Number guessing game

import random def guessing_game(): answer = random.randint(0, 100) while True: guess = int(input('Guess the number: ')) if guess < answer: print('Too low') elif guess > answer: print('Too high') else: print('Just right') break guessing_game() section 1.2 section 1.3

import random
number = random.randint(10, 30)

Shows the minimal import-and-call pattern for assigning a random integer to a variable. section 1.2

How Python Workout: 50 ten-minute exercises covers it

How deep it goes
Explains importing random, passing two bounds, and remembering that the maximum is inclusive.
Examples
Uses a 0–100 guessing game and a random.randint(10, 30) assignment.
What you can do after
Build it from scratch by generating a bounded integer and using it in an interactive guessing loop.
How it connects
Connects randint with the random module, comparison operators, while loops, input, and integer conversion.

Also explained in

Compare how 2 books explain randint →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Workout: 50 ten-minute exercises. The section links point to the parts of the book it draws on.