Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
No results
  • About AskPython
  • Blog
  • Contact Me
  • Free Online Developer Tools Hub
  • Home
  • Meet the Team
  • Privacy Policy
  • Python
  • Python Course for Beginners Online FREE
  • Python Tutorial – Mega Index
  • Terms and Conditions

🚀 Supercharge your YouTube channel's growth with AI.

Try YTGrowAI Free
  • Home
  • Blog
  • About AskPython
  • Python Course
  • Start Here

Numpy Random.rand() – Generate Random Data using Numpy

  • Adrita DasAdrita Das
  • February 27, 2022
  • NumPy
Numpy Random.rand() – Generate Random Data using Numpy

Random. rand() function can help you generate random data in a very simple way. In this article, we will learn how to generate random values using the random.rand() function. So let’s learn about it.

What Is the random.rand() Function?

The random.rand() function is used to return randomly generated values in a given shape. The function returns an array that has the shape as specified and fills the array with random values which are normally distributed in the range [0,1].

Syntax of the random.rand() Function

numpy.random.rand(d0, d1, …, dn)

ParameterDescription
d0,d1,…..,dnThe dimensions of the array.

For example:

import numpy as np

x = np.random.rand()
print(x)

Output:

0.2967574962954477

You can also incorporate the seed() function into the random.rand () function to generate output that will remain constant with every run.

import numpy as np

np.random.seed(0)

x = np.random.rand()
print(x)

Output:

0.5488135039273248

Let’s see how we can generate 1-D and 2-D arrays with the help of the Numpy random.rand() function.

1-D Array with np.random.rand() function

The following code will return a 1-D array of the specified shape.

import numpy as np

np.random.seed(0)

x = np.random.rand(6)
print(x)

Output:

[0.5488135  0.71518937 0.60276338 0.54488318 0.4236548  0.64589411]

If you want to generate more range of numbers, use the following code.

import numpy as np

np.random.seed(0)

x = np.random.rand(6)*10
print(x)

Output:

[5.48813504 7.15189366 6.02763376 5.44883183 4.23654799 6.45894113]

2-D array with the np.random.rand () function

The following code will generate a 2-D array.

import numpy as np

np.random.seed(0)

x = np.random.rand(2,3)
print(x)

Output:

[[0.5488135  0.71518937 0.60276338]
 [0.54488318 0.4236548  0.64589411]]

Multi-dimensional array with np.random.rand() function

The following code will generate arrays of higher dimensions.

import numpy as np

np.random.seed(0)

x = np.random.rand(2,4,2,4)

print(x)

Output:

[[[[0.5488135  0.71518937 0.60276338 0.54488318]
   [0.4236548  0.64589411 0.43758721 0.891773  ]]

  [[0.96366276 0.38344152 0.79172504 0.52889492]
   [0.56804456 0.92559664 0.07103606 0.0871293 ]]

  [[0.0202184  0.83261985 0.77815675 0.87001215]
   [0.97861834 0.79915856 0.46147936 0.78052918]]

  [[0.11827443 0.63992102 0.14335329 0.94466892]
   [0.52184832 0.41466194 0.26455561 0.77423369]]]


 [[[0.45615033 0.56843395 0.0187898  0.6176355 ]
   [0.61209572 0.616934   0.94374808 0.6818203 ]]

  [[0.3595079  0.43703195 0.6976312  0.06022547]
   [0.66676672 0.67063787 0.21038256 0.1289263 ]]

  [[0.31542835 0.36371077 0.57019677 0.43860151]
   [0.98837384 0.10204481 0.20887676 0.16130952]]

  [[0.65310833 0.2532916  0.46631077 0.24442559]
   [0.15896958 0.11037514 0.65632959 0.13818295]]]]

Conclusion

In this article, you learned how to generate arrays of one dimension, two-dimension, and also higher dimension using the np.random.rand() function.Hope you found this article helpful.

Adrita Das
Adrita Das
Articles: 24
4 Methods to Check Whether a String Is Empty in Python
Previous Post 4 Methods to Check Whether a String Is Empty in Python
Next Post T-Test Hypothesis in Python
T-Test Hypothesis in Python

Related Posts

np.linalg.eig: Eigenvalues and Eigenvectors

np.linalg.eig: Eigenvalues and Eigenvectors

  • November 30, 2026
np.sin() in NumPy: Radians, Arrays, and Examples

How to Use np.sin() in NumPy

  • November 30, 2026
np.outer(): Outer Product in NumPy

np.outer(): Outer Product in NumPy

  • November 14, 2026

Partner Websites

  • GoLangDocs
  • LinuxForDevices
  • CodeForGeek

Popular Categories

  • Python Programming
  • Python Modules
  • OOPS in Python
  • Python Programming Examples
  • Strings in Python

Hall of Fame

  • Python Programming
  • OOPS in Python
  • Creating Python Modules
  • Args and Kwargs in Python
  • Python Dictionary (Dict) Tutorial
  • Python List – 15 Things You MUST Know
  • Python Set – Things You MUST Know
  • Python String Functions: Methods by Task

Important Links

  • Home
  • Python Course
  • Start Here
  • Blog
  • About AskPython
Latest posts
  • np.linalg.eig: Eigenvalues and Eigenvectors
  • What Does %s Mean in a Python Format String?
  • How to Use np.sin() in NumPy
  • cv2.imread: Load Images with OpenCV
  • pd.read_json: Read JSON Into a DataFrame

Copyright © 2026 - AskPython. All rights reserved.