Numpy Tutorial
Creating NumPy Array
NumPy Array Manipulation
Matrix in NumPy
Operations on NumPy Array
Reshaping NumPy Array
Indexing NumPy Array
Arithmetic operations on NumPy Array
Linear Algebra in NumPy Array
NumPy and Random Data
Sorting and Searching in NumPy Array
Universal Functions
Working With Images
Projects and Applications with NumPy
numpy.random.random_integers()
is a function from the numpy
library that generates random integers. However, numpy.random.random_integers()
is deprecated and has been replaced by numpy.random.randint()
. Therefore, We'll provide a tutorial for numpy.random.randint()
, which serves a similar purpose.
numpy.random.randint()
function tutorialFunction Signature:
numpy.random.randint(low, high=None, size=None, dtype='l')
Parameters:
high
is not provided, then the interval is [0, low
).low
becomes the upper limit, and the lower limit is set to 0.size
is not provided, a single integer is returned.Examples:
import numpy as np random_int = np.random.randint(11) print(random_int)
random_int = np.random.randint(5, 11) print(random_int)
random_array = np.random.randint(0, 11, size=5) print(random_array)
random_matrix = np.random.randint(0, 11, size=(3, 3)) print(random_matrix)
random_array = np.random.randint(0, 11, size=5, dtype='int8') print(random_array)
Notes:
numpy.random.randint()
and not the deprecated numpy.random.random_integers()
.numpy.random.seed(seed_value)
if you want reproducible results.Using numpy.random.randint()
, you can easily generate random integers for various applications, such as simulations, random sampling, and more.
Random integer generation involves creating random integers using NumPy's randint()
function.
import numpy as np # Generate a random integer between 1 and 10 random_integer = np.random.randint(1, 11) # Print the random integer print("Random Integer:", random_integer)
numpy.random.randint()
for random integer generation:Learn how to use NumPy's randint()
function for generating random integers.
import numpy as np # Generate a random integer between 5 and 20 random_integer = np.random.randint(5, 21) # Print the random integer print("Random Integer:", random_integer)
randint()
function for random integer sampling:Explore the usage of NumPy's randint()
function specifically designed for random integer sampling.
import numpy as np # Generate a random integer between 10 and 50 random_integer = np.random.randint(10, 51) # Print the random integer print("Random Integer:", random_integer)
numpy.randint()
usage for random sampling:Learn how to use the numpy.randint()
function in Python for random integer sampling.
import numpy as np # Generate a random integer between 1 and 100 random_integer = np.random.randint(1, 101) # Print the random integer print("Random Integer:", random_integer)
A sample code demonstrating random integer sampling using NumPy in Python.
import numpy as np # Generate an array of 5 random integers between 0 and 9 random_integers = np.random.randint(0, 10, size=5) # Print the random integers print("Random Integers:", random_integers)
Understand the process of generating random integers using NumPy in Python.
import numpy as np # Generate an array of 3 random integers between 50 and 100 random_integers = np.random.randint(50, 101, size=3) # Print the random integers print("Random Integers:", random_integers)
randint
vs random.choices
for random sampling:Compare NumPy's randint
with random.choices
for random integer sampling.
import numpy as np import random # Using NumPy randint random_integers_np = np.random.randint(1, 11, size=5) # Using random.choices random_integers_choices = random.choices(range(1, 11), k=5) # Print the random integers print("Random Integers (NumPy):", random_integers_np) print("Random Integers (choices):", random_integers_choices)
numpy.random.randint()
for array randomization:Utilize numpy.random.randint()
to randomize the order of elements in a NumPy array.
import numpy as np # Create an array to be randomized original_array = np.array([10, 20, 30, 40, 50]) # Randomize the order of elements using NumPy randint randomized_array = np.random.randint(original_array.min(), original_array.max() + 1, size=len(original_array)) np.random.shuffle(randomized_array) # Print the randomized array print("Original Array:", original_array) print("Randomized Array:", randomized_array)