If you just need sampling without replacement:

>>> import random
>>> random.sample(range(1, 100), 3)
[77, 52, 45]

random.sample takes a population and a sample size k and returns k random members of the population.

If you have to control for the case where k is larger than len(population), you need to be prepared to catch a ValueError:

>>> try:
...   random.sample(range(1, 2), 3)
... except ValueError:
...   print('Sample size exceeded population size.')
... 
Sample size exceeded population size
Answer from Two-Bit Alchemist on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-generate-random-numbers-within-a-given-range-and-store-in-a-list
Generate random numbers within a given range and store in a list - Python - GeeksforGeeks
May 5, 2025 - The size=no tells it how many to create and .tolist() simply turns the result into a regular Python list. This approach applies the random.randint() function over a range using Python’s built-in map() function.
🌐
Python documentation
docs.python.org › 3 › library › random.html
random — Generate pseudo-random numbers
Almost all module functions depend on the basic function random(), which generates a random float uniformly in the half-open range 0.0 <= X < 1.0. Python uses the Mersenne Twister as the core generator.
🌐
Data Science Discovery
discovery.cs.illinois.edu › learn › Simulation-and-Distributions › Random-Numbers-in-Python
Random Numbers in Python - Data Science Discovery
Generating a random number from a range with random.randint, and · Choosing a random element from a Python list with random.choice · One of the primary ways we generate random numbers in Python is to generate a random integer (a whole number) within a specified range.
🌐
Enki
enki.com › post › get-random-number-in-python
Enki | Blog - How to generate random numbers in Python
To generate a random integer within a range, use the random.randint(a, b) function.
🌐
Python
pythonprogramminglanguage.com › randon-numbers
Generate Random Numbers using Python - Python
The function random.random(). The ... call the random() method to generate a real (float) number between 0 and 1. This outputs any number between 0 and 1. For most apps, you will need random integers instead of numbers between 0 and 1. The function randint() generates random integers for you. If you call the function, it returns a random integer N such that a <= N <= b. The randint() method to generates a whole number (integer)....
Find elsewhere
🌐
Hyperskill
hyperskill.org › university › python › random-integer-generation-in-python
Random Integer Generation in Python
July 18, 2024 - To create whole numbers you can utilize the randint() function offered by the random module in Python. This function enables you to generate an integer within a specified range.
🌐
Python Central
pythoncentral.io › how-to-generate-a-random-number-in-python
How to Generate a Random Number in Python | Python Central
February 5, 2025 - This function is inclusive of both the endpoints of the given range. The syntax of this code is the same as those mentioned for previous functions. ... The output of this code is a floating-point number. ... To obtain the output as an integer, we can specially typecast the uniform() function. ... Typecasting the function gives integer output. The numpy module also has the sub-module random.
🌐
W3Schools
w3schools.com › python › ref_random_randrange.asp
Python Random randrange() Method
Remove List Duplicates Reverse ... Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... The randrange() method returns a randomly selected element from the specified range....
🌐
Codeguage
codeguage.com › v1 › courses › python › numbers-random-numbers
Python Random Numbers - The rand() Function | Codeguage
Using the random() function and a bit of arithmetic, one can generate random numbers in any given range.
🌐
Replit
replit.com › home › discover › how to generate random numbers in python
How to generate random numbers in Python
This means the returned value can be the start number, the end number, or any integer in between. So, random.randint(1, 100) will give you a random integer from 1 to 100, including both endpoints. It’s a straightforward way to get a whole ...
🌐
IONOS
ionos.com › digital guide › websites › web development › python randint
What is the Python randint() function? - IONOS
July 26, 2024 - start: The initial value of the range where the random integer is generated. This number is included in the result. end: This is the final value of the interval. This number is also included in the result. ... This code produces a random whole number between 1 and 100 and displays it on the console.
🌐
Hero Vired
herovired.com › learning-hub › blogs › random-number-generator-python
Random Number Generator Python between 1 and 10 | Hero Vired
Syntax: # generate random Gaussian values from random import seed from random import gauss # seed random number generator seed(1) # generate some Gaussian values for _ in range(10): value = gauss(0, 1) print(value) Outcome: 1.2881847531554629 1.449445608699771 0.06633580893826191 -0.7645436509716318 -1.0921732151041414 0.03133451683171687 -1.022103170010873 -1.4368294451025299 0.19931197648375384 0.13337460465860485 · The random number generator Python also uses the uniform distribution inversion method to deliver outcomes.
🌐
Vultr
docs.vultr.com › python › examples › generate-a-random-number
Python Program to Generate a Random Number | Vultr Docs
December 9, 2024 - Use the randint function to generate a random integer within a specified range. ... This code generates a random integer between 1 and 10, inclusive. The randint function takes two parameters, the lower and upper limits of the range, making ...
🌐
Shiksha
shiksha.com › home › it & software › it & software articles › programming articles › how to generate random numbers in python?
How to Generate Random Numbers in Python? - Shiksha Online
September 6, 2024 - Then, we call the randrange() function, which takes the start=20, stop=50, and step=4 as parameters in this case. The function generates a random element from the list [24,28,32,36,40,44,48] that is obtained from the specified range.
🌐
DEV Community
dev.to › hichem-mg › python-generate-random-integers-in-range-4m0d
Python: Generate Random Integers in Range - DEV Community
June 11, 2024 - Python’s random module is part of the standard library, providing various functions to generate random numbers, including integers, floats, and more. This module uses the Mersenne Twister algorithm, which is a pseudo-random number generator. While it might sound complex, using it is straightforward and powerful. import random # Generate a random float number between 0 and 1 random_float = random.random() print(random_float)