• 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 / Numpy Tips / How to generate random numbers from Beta distribution in Numpy

How to generate random numbers from Beta distribution in Numpy

December 30, 2022 by cmdlinetips

In this tutorial, we will learn how to generate random numbers from Beta distribution in Python using Numpy. We will use Numpy’s Random Generator object with. random module’s beta() function to generate random numbers from beta distribution.

We will start with learning the basics of Beta distribution. Then we will learn how to generate a single random number, a 1_D array of random numbers, and 2-D array or matrix of random numbers from Beta distribution using Numpy’s beta() function.

What is Beta Distribution

The beta distribution is a continuous probability distribution that is defined on the interval [0, 1] and is commonly used to model the probability of success in a binomial process (i.e., a series of independent, two-outcome events). It is parameterized by two shape parameters, alpha and beta, which control the shape of the distribution.

The beta distribution is useful/popular for a few reasons. Since it is defined on the interval [0, 1], it is very well suited for modeling probabilities. And also depending on the values of alpha and beta, it is flexible to change the shape of Beta distribution. Here is an example of different shapes t of beta distribution and their corresponding parameters.

Shapes of Beta distribution and their parameters
Shapes of Beta distribution and their parameters

Let us get started by loading Numpy.

import numpy as np

The basic syntax of generating random number in Numpy is this, where a, b are the parameters alpha and beta and it controls shape of the beta distribution.

random.Generator.beta(a, b, size=None)

Generating a random number from beta distribution

We will generate a random number from three different set of parameters alpha (a) and beta(b).

First let us set the random generator object by instantiating using default_rng() function with a seed.

rng = np.random.default_rng(2022)

We can generate a random number with a = 2 and b = 2

rng.beta(a=2, b=2)

0.1706219925464454

Here we generate a random number with a = 1 and b = 5 using Numpy’s beta() function.

rng.beta(a=1, b=5)

0.19496266482763683

Here we generate a random number with a = 5 and b = 1 using Numpy’s beta() function.

rng.beta(a=5, b=1)

0.9043150823003117

Generating a 1-D array of random numbers from Beta distribution

Here we use the same shape parameters a and b as before, but this time we specify the argument size=10 to generate 10 random numbers from beta distribution.

rng.beta(a=2, b=2,  size=10)

array([0.57780648, 0.35569291, 0.41385092, 0.60801077, 0.68471871,
       0.5471684 , 0.91006246, 0.60178419, 0.78839202, 0.37842288])

Here the 1-d array with random numbers from beta distribution is generated using a =5 and b=1.

rng.beta(a=5, b=1,  size=10)

array([0.92888649, 0.97324572, 0.98370505, 0.8273475 , 0.90235001,
       0.82462658, 0.97997441, 0.9662021 , 0.81329731, 0.92557757])

Here the 1-d array with random numbers from beta distribution is generated using a =1 and b=5.

rng.beta(a=1, b=5,  size=10)

array([0.06307234, 0.18047868, 0.00069429, 0.30016567, 0.16456846,
       0.06142862, 0.10499444, 0.0668597 , 0.24838117, 0.0554329 ])

In both of the examples above we can clearly see the trend of skewing towards 1 and 0 is clear.

Generating a 2-D array of random numbers from Beta distribution

In these examples below, we show how to generate a matrix of dimension 3×3 with random numbers samples from beta distribution.

First, we use a=1 and b=5 as shape parameters to generate random numbers between 0 to 1, but skewed towards 0 with Numpy’s beta() function.

rng.beta(a=1, b=5,  size=(3,3))

array([[0.09601718, 0.22013485, 0.3938391 ],
       [0.13066181, 0.03355825, 0.13609113],
       [0.07612266, 0.05309854, 0.12993868]])

First, we use a=5 and b=1 as shape parameters to generate random numbers between 0 to 1, but skewed towards 1 with Numpy’s beta() function.

rng.beta(a=5, b=1,  size=(3,3))
array([[0.54353228, 0.69803459, 0.98964326],
       [0.91476447, 0.85729941, 0.61320125],
       [0.95792108, 0.95349669, 0.80066977]])

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 binomial distribution in Numpy Default ThumbnailHow to generate random numbers from Normal distribution in Numpy Default ThumbnailGenerate Random Integers using Generators in Numpy

Filed Under: Numpy Tips Tagged With: Numpy beta() function

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

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

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

Go to mobile version