Python Numpy – Create Array with Random Values

Python is a popular and innovative language for programmers. It provides the latest libraries and modules used for various purposes, such as the “scikit-learn” library used in machine learning algorithms, “Opencv” is used for computer vision, etc. Similarly, the “Numpy” library in Python provides various functions to work with Arrays.

In this Python guide, we will explain how to create arrays with random values in Python. The following aspects are explained in detail with appropriate examples:

So let’s begin!

How to Create an Array With Random Values?

In Python, different inbuilt functions are used to create an array with random values, such as “Numpy.empty()”, “rand()”, and “randint()” functions. These functions create arrays of different sizes, shapes, or dimensions. The “rand()” and “randint()” functions must include the keyword “random” at the start because these functions are part of the random module.

Let’s see various examples and learn how to create an array with different random values:

Example 1: Using Numpy.empty() Method

The “NumPy.empty()” function of the Numpy library allows the user to create an array of random values in a python program. The syntax of “NumPy.empty()” is shown below:

numpy.empty(shape, dtype=float, order='C')

Let’s comprehend the above syntax stepwise:

  • The parameter “shape” is used to define the array shape such as (2,3),(2,2), etc.
  • The parameter “dtype” (optional) is used to initialize the data type of the array(it’s float by default).
  • The last optional parameter, “order”, defines the order of a multidimensional array. The default value of this order parameter is “row-major” (C-style).

Lets understand the concept of this function via the following example:

Code:

# Create an array with random values
import numpy as np

var_val = np.empty(3, dtype = int)
print(var_val)

var_val1 = np.empty([2, 3], dtype = int)
print("\n", var_val1)

In the above example code:

  • The “NumPy” library is imported as “np” in the program.
  • The “np.empty()” function of the NumPy library takes two parameters. The first parameter indicates the shape of the array, while the second parameter takes the data type, which is “int” in our case.
  • The “np.empty()” function is again used in the program to create a “2×3” array matrix of integer data type.

Output:

The output shows the value of arrays created with random values.

Example 2: Using rand() Function to Create Array with Random Values

The “rand()” function creates 1-Dimensional, 2-Dimensional, and 3-Dimensional arrays with random values. Defining the array size inside the function is optional. If none of the sizes is defined, then by default, this function will return a single floating point value. Let’s understand this function via the following examples:

Code:

import numpy as np

# Array created with random values
random = np.random.rand(3)
random1 = np.random.rand(3,4)
print('1st Array',random)
print('\n2nd Array',random1)

In the following code:

  • The NumPy library package is imported in the program.
  • The “rand()” function creates an array with a default float type random value. The “rand()” function takes the dimension size as an argument.
  • The “rand()” function is used to generate an array of a single row with “3” random values.
  • Similarly, the “rand()” function takes the dimension (3,4) as an argument to create an array of random values in a “3×4” matrix.

Output:

The output shows a 1-D and 2-D array with random values.

Example 3: Using randint() Function to Create Array With Random Values

The “randint()” function is used to create an array containing random values. This function returns the random integer values as an output. The array shape can be defined inside this function as an argument value. The following code shows the concept of the “randint() function”:

Code:

import numpy as np

random_1 = np.random.randint(15,30)
print("First array with random value", random_1)

random_2 = np.random.randint(0,50,(2,4))
print("\nSecond array with random value", random_2)

In the mentioned code:

  • The NumPy library is imported as np in the program.
  • The “randint()” function generates an array containing random values. The first parameter is the starting limit of an integer number, and the second number is the end limit of the number. The random value of the array will be generated within these two number limits.
  • In the second “randint()” function, the shape/dimension of the arrays is also defined in the third parameter, i.e., (2×4).

Output:

The output shows the random value within the 1D and 2D arrays.

That’s all from this Python Tutorial!

Conclusion

In Python, the “rand()”, “randint()” and “np.empty()” functions create an array with random values. These functions create “1-D”, “2-D”, and “3-D” arrays with different random values. The “rand()” function takes the dimension size as input and returns the random float value as output. The “randint()” function takes the dimension size and creates arrays of random values between the specified range. This blog post explained all the methods with numerous examples to create an array with random values.