Python for Introductory StatisticsXYZ Homework Edition

⇩ Download ▾

Appendix 2: Random Integer Generator

Watch demo video

"A random number generator, generates a sequence of numbers or symbols that cannot be reasonably predicted better than by a random chance." Wikipedia

To randomly generate nn integers between aa and bb, inclusive, use the following function from the numpy library. Here n,an,a and bb are integers.

import numpy as np  
np.random.randint(a, b+1, size=n)

The above code returns an array of nn random integers from aa to b+1b+1, inclusive. Notice that every time you run the program, it will generate different sets of numbers. To create the same set of random numbers you can add the line np.random.seed(123).

Example: Write a program to randomly select one integer between 1 and 10.

Adapted from Python for Introductory Statistics, by Simon Aman (Truman College, City Colleges of Chicago), licensed under CC BY 4.0. Changes were made: reformatted as an accessible XYZ web edition with live in-browser code cells. License: CC-BY-4.0.

These eBooks are a prerelease and are not yet certified conformant with WCAG 2.1 AA or ADA Title II. Every page is built against an automated accessibility gate, and the published editions will meet ADA Title II requirements when they release in late September 2026. If something is unusable, please tell us.