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

Random sampling in numpy | random_integers() function

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 tutorial

Function Signature:

numpy.random.randint(low, high=None, size=None, dtype='l')

Parameters:

  • low: Start of interval. The result includes this value. If high is not provided, then the interval is [0, low).
  • high: End of the interval. The result does not include this value. If this parameter is not provided, then low becomes the upper limit, and the lower limit is set to 0.
  • size: The shape of the output. It could be an integer or a tuple of integers. If size is not provided, a single integer is returned.
  • dtype: Data type of the result.

Examples:

  • Generate a single random integer between 0 and 10 (inclusive):
import numpy as np
random_int = np.random.randint(11)
print(random_int)
  • Generate a single random integer between 5 and 10 (inclusive):
random_int = np.random.randint(5, 11)
print(random_int)
  • Generate an array of 5 random integers between 0 and 10 (inclusive):
random_array = np.random.randint(0, 11, size=5)
print(random_array)
  • Generate a 3x3 matrix of random integers between 0 and 10 (inclusive):
random_matrix = np.random.randint(0, 11, size=(3, 3))
print(random_matrix)
  • Generate an array of random integers with a specific data type:
random_array = np.random.randint(0, 11, size=5, dtype='int8')
print(random_array)

Notes:

  • Always ensure you're using numpy.random.randint() and not the deprecated numpy.random.random_integers().
  • You can set a seed using 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.

1. Random integers in Python with NumPy:

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)

2. How to use 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)

3. Numpy 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)

4. Python 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)

5. Sample code for random integer sampling in NumPy:

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)

6. Generating random integers with NumPy in Python:

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)

7. Numpy 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)

8. Python 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)