Generate random integers within a range in Python
Python is a very useful tool for data analysis. When we deal with real-life situations, we have to generate random values to simulate the situation and process it.
random
Python has the and modules
available NumPy
which have efficient methods to work and generate random numbers easily.
In this tutorial, we will generate some random integers between a specific range in Python.
random.randint()
Generate random integers between a specific range
using the function in Python
randint()
The function is used to generate random integers between a specified range. The starting and ending positions are passed as parameters to the function.
For example,
import random
x = random.randint(0, 10)
print(x)
Output:
8
To use this function to generate a list of random numbers, we can for
use the list comprehension method with a loop, like this:
import random
x = [random.randint(0, 9) for p in range(0, 10)]
print(x)
Output:
[1, 6, 6, 5, 8, 8, 5, 5, 8, 4]
Note that this method accepts only integer values.
random.randrange()
Generate random integers between a specific range
using the function in Python
randrange()
The function also returns a random number within a range and only accepts integer values, but here we have the option to specify a very useful parameter called step
. step
The parameter allows us to find random numbers that are divisible by a specific number. By default, this parameter is 1.
For example,
import random
x = random.randrange(0, 10, 2)
print(x)
Output:
4
Notice that the output is divisible by 2. Using the same list comprehension method, we can use this function to generate a list of random numbers as shown below.
import random
x = [random.randrange(0, 10, 2) for p in range(0, 10)]
print(x)
Output:
[8, 0, 6, 2, 0, 6, 8, 6, 0, 4]
random.sample()
Generate random integers between a specific range
using the function in Python
Using this function, we can specify the range and total number of random numbers to be generated. It also ensures that there are no duplicate values. The following example shows how to use this function.
import random
x = random.sample(range(10), 5)
print(x)
Output:
[7, 8, 5, 9, 6]
NumPy
Generate random integers between a specific range
in Python using the module
NumPy
The module also has three functions that can be used to accomplish this task and generate the required number of random integers and store them in a numpy array.
These functions are numpy.random.randint()
, , numpy.random.choice()
and numpy.random.uniform()
. The following code shows how to use these functions.
For example,
import numpy as np
x1 = np.random.randint(low=0, high=10, size=(5,))
print(x1)
x2 = np.random.uniform(low=0, high=10, size=(5,)).astype(int)
print(x2)
x3 = np.random.choice(a=10, size=5)
print(x3)
Output:
[3 2 2 2 8]
[8 7 9 2 9]
[0 7 4 1 4]
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Implementing a Low-Pass Filter in Python
Publish Date:2025/05/07 Views:89 Category:Python
-
Low pass filter is a term in signal processing basics and is often used to filter signals to obtain more accurate results. This tutorial will discuss the low-pass filter and how to create and implement it in Python. A low-pass filter is use
Implementing Curl command in Python using requests module
Publish Date:2025/05/07 Views:97 Category:Python
-
requests This article will discuss and implement different curl commands using the module in Python . requests Installing modules in Python Python provides us with requests the module to execute curl command. Install it in Python 3 using Pi
Using fetchall() in Python to extract elements from a database
Publish Date:2025/05/07 Views:171 Category:Python
-
This article aims to describe fetchall() the working methods of extracting elements from a database using and how to display them correctly. This article will also discuss list(cursor) how functions can be used in programs. fetchall() Extra
Parsing log files in Python
Publish Date:2025/05/07 Views:106 Category:Python
-
Log files contain information about events that occurred during the operation of a software system or application. These events include errors, requests made by users, bugs, etc. Developers can further scan these usage details to find poten
Declaring a variable without a value in Python
Publish Date:2025/05/07 Views:57 Category:Python
-
A variable is a reserved memory location that can store some value. In other words, variables in a Python program provide data to the computer to process operations. Every value in Python has a data type. There are numbers, lists, tuples, e
Defining class global variables in Python
Publish Date:2025/05/07 Views:81 Category:Python
-
A global variable is a variable that is visible and available in every part of the program. Global variables are also not defined in any function or method. On the other hand, local variables are defined in functions and can be used only in
Incrementing loop step by 2 in Python
Publish Date:2025/05/07 Views:199 Category:Python
-
In each iteration, for the loop increases the counter variable by a constant. A loop with the sequence 0, 2, 4, 6 for will increase the counter variable by 2 each iteration. This article will show you some for ways to increment by 2 in a lo
Pool map with multiple parameters in Python
Publish Date:2025/05/07 Views:104 Category:Python
-
multiprocessing This article will explain different ways to perform parallel function execution using the module in Python . multiprocessing The module provides functionality to perform parallel function execution using multiple inputs and
Python if...else in Lambda function
Publish Date:2025/05/07 Views:68 Category:Python
-
lambda Functions are used to implement some simple logic in Python and can be thought of as anonymous functions. It can have multiple parameters but only one expression, just def like any other function defined using the keyword. We can def