Specify a NumPy dtype when generating random values

Learn, how can we specify a NumPy dtype when generating random values?
By Pranit Sharma Last updated : December 27, 2023

Problem statement

Suppose that we need to create a numpy array using some random values with a specified data type.

Specifying a NumPy dtype when generating random values

To generate random values, we can use numpy.random.randn() which is especially used to return a sample (or samples) from the "standard normal" distribution.

It returns a ndarray (d0, d1, ..., dn) shaped array of floating-point samples from the standard normal distribution, or a single such float if no parameters were supplied.

Let us understand with the help of an example,

Python code to specify a NumPy dtype when generating random values

# Import numpy
import numpy as np

# Creating a numpy array using random values
arr = np.random.rand(2,3)

# Display original data
print("Original data:\n",arr,"\n")

Output

Example: Specify a NumPy dtype when generating random values

In this example, we have used the following Python basic topics that you should learn:

Python NumPy Programs »

Comments and Discussions!

Load comments ↻





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