×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Non-repetitive random number in NumPy

Learn, how to generate a non-repetitive random series in numpy? By Pranit Sharma Last updated : October 09, 2023

NumPy is an abbreviated form of Numerical Python. It is used for different types of scientific operations in python. Numpy is a vast library in python which is used for almost every kind of scientific or mathematical operation. It is itself an array which is a collection of various methods and functions for processing the arrays.

NumPy - Generating a non-repetitive random series

The numpy.random() method is used to generate some random numbers which may get repeated but here, we need to generate a non-repetitive random number series.

In numpy random module, we have a method default_rng() which will return an object, this object contains a method called choice() with the help of which we can create a random number series and if we pass an argument called replace=False, it will create a non repetitive random number series.

Let us understand with the help of an example,

Python program to generate a non-repetitive random series in numpy

# Import numpy
import numpy as np

# Import default_rng
from numpy.random import default_rng

# Creating an object of default_rng
obj = default_rng()

# Creating non repetitive random series
res = obj.choice(20, size=10, replace=False)

# Display result
print("Non Repetitive random series:\n",res)

Output

The output of the above program is:

Example: Non-repetitive random number in NumPy

Python NumPy Programs »

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.