Specify a NumPy dtype when generating random values

Learn, how can we specify a NumPy dtype when generating random values?
Submitted by Pranit Sharma, on March 20, 2023

Specifying a NumPy dtype when generating random values

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

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

Python NumPy Programs »





Comments and Discussions!










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