• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Python / Generate Random Integers using Generators in Numpy

Generate Random Integers using Generators in Numpy

December 25, 2022 by cmdlinetips

In this post, we will learn how to generate random integers using Random Generators in Numpy Python. Random Generator class in Numpy offers wide variety of random distributions including integers.

To get started let us import Numpy

import numpy as np

To generate random numbers, we need a construcor of random number class Generator. In Numpy, default_rng() is the recommended constructor for generating random numbers.

Example 1: Generating a random integer

As a first example, let us generate a random integer. For generating random number, we need to create an instance of Random Generator using default_rng() as shown below.

# an instance of random. generator
rng = np.random.default_rng()

Now we can use the instance to generate any random number we want. Here we will generate a random integer using integers() function.

To generate a random integer between 0 and 9 (inclusive), we need to specify low and high arguments to integers() function. each time the code is run. In this example, our low is 0 and. high is 10. And this will generate a random integer between 0 and 9, including 9.

rng.integers(low=0, high=10)
8

Example 2: Generating an array of random integers

Similarly to generate an array of N random integers, we need to specify N using size argument inn addition to low and high argument. Note we can use the same random generator instance.

Here we have generated a 1D array of size 5.

rng.integers(low=0, high=10, size=5)
array([4, 1, 9, 9, 9])

In another example, we are generating 10 random integers between 0 and 9.

np.random.randint(0, 10, size=10)
array([2, 2, 6, 1, 3, 9, 6, 1, 0, 1])

Example 3: Generating a 2D array of random integers

By specifying the size argument to integers() function as tuple we can generate 2D array of random integers. Here the first element of the tuple specifies the number of rows and the second element specifies the number of columns.
In the example below, we generate matrix of random integers of dimension 3 x 5 with numbers between 0 and 9.

rng = np.random.default_rng()
rng.integers(low=0, high=10, size=(3,5))

[[9 6 1 0 1]
 [9 0 0 9 3]
 [4 0 0 4 1]]

With integers() function, we can generate 3DD Numpy array as well. In the example below we generate a 3D matrix with three element tuple for the size argument.

random_3D_array = rng.integers(0, 10, size=(2,3,5))
print(random_3D_array)
[[[2 7 8 8 8]
  [0 5 2 2 2]
  [8 7 2 4 7]]

 [[1 6 4 9 7]
  [2 8 7 2 5]
  [7 2 2 1 0]]]

Example 4: Generating a random integers with seed

Oftenw hen we generate random numbers, we would like to reproduce the same results. To reproduce same random numbers we need to specify a random seed, an integer, as argument to the constructor default_rng() function.

In. the. examples below we use seed=9 to generate a random integer

rng = np.random.default_rng(seed=9)
rng.integers(low=0,high=10)
9

to generate a 1D array of random integers

rng = np.random.default_rng(seed=9)
rng.integers(low=0,high=10, size=5)
array([4, 8, 9, 2, 1])

and to generate a. 2D array of random integers.

rng = np.random.default_rng(seed = 9)
rng.integers(low=0,high=10, size=(3,5))

array([[0, 6, 5, 0, 9],
       [2, 2, 1, 3, 1],
       [3, 8, 4, 9, 4]])

By using the same seed will generate the same random integers when we run the code again.

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default ThumbnailHow to generate random numbers from Poisson distribution with Numpy Default ThumbnailHow to generate random numbers from Normal distribution in Numpy Default ThumbnailHow to Randomly permute Numpy Array Default ThumbnailHow to do Cholesky Matrix Decomposition with Numpy

Filed Under: Numpy Tips, Python, Python Tips Tagged With: Numpy default_rng(), Numpy random integers

Reader Interactions

Trackbacks

  1. How to do Cholesky Matrix Decomposition with Numpy - Python and R Tips says:
    December 27, 2022 at 10:58 pm

    […] Let us consider another example of performing Cholesky decomposition in Python using Numpy. This time we will start with a matrix with randomly generated integers. To generate random integers we will use Numpy’s Random generator class with integers() function. […]

  2. SVD with Numpy - Python and R Tips says:
    December 28, 2022 at 10:31 pm

    […] the second example, we will decompose a 4×4 matrix created by using Numpy’s integers() function that generates random integers. Here we first generate random integers and the use Numpy reshape() function to make it a 4×4 […]

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version