×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

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 »

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

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