HomeLanguagesnumpy.random.rand() in Python

numpy.random.rand() in Python

The numpy.random.rand() function creates an array of specified shape and fills it with random values. Syntax : 

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

Parameters : 

d0, d1, ..., dn : [int, optional]Dimension of the returned array we require, 
     If no argument is given a single Python float is returned.

Return : 

Array of defined shape, filled with random values.

Code 1 : Randomly constructing 1D array 

Python




# Python Program illustrating
# numpy.random.rand() method
  
import numpy as geek
  
# 1D Array
array = geek.random.rand(5)
print("1D Array filled with random values : \n", array);


Output : 

1D Array filled with random values : 
 [ 0.84503968  0.61570994  0.7619945   0.34994803  0.40113761]

Code 2 : Randomly constructing 2D array 

Python




# Python Program illustrating
# numpy.random.rand() method
  
import numpy as geek
  
# 2D Array  
array = geek.random.rand(3, 4)
print("\n\n2D Array filled with random values : \n", array);


Output : 

2D Array filled with random values : 
 [[ 0.94739375  0.5557614   0.69812121  0.86902435]
 [ 0.94758176  0.22254413  0.21605843  0.44673235]
 [ 0.61683839  0.40570269  0.34369248  0.46799524]]

Code 3 : Randomly constructing 3D array 

Python




# Python Program illustrating
# numpy.random.rand() method
  
import numpy as geek
  
# 3D Array    
array = geek.random.rand(2, 2 ,2)
print("\n\n3D Array filled with random values : \n", array);


Output : 

3D Array filled with random values : 
 [[[ 0.97942627  0.01068711]
  [ 0.35749073  0.22484643]]

 [[ 0.99733022  0.8029555 ]
  [ 0.44111692  0.90537128]]]

References : https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.random.rand.html#numpy.random.rand 

Note : These codes won’t run on online IDE’s. So please, run them on your systems to explore the working. 

This article is contributed by Mohit Gupta_OMG 😀. If you like Lazyroar and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to [email protected]. See your article appearing on the Lazyroar main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32706 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7080 POSTS0 COMMENTS
Nicole Veronica
12216 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12305 POSTS0 COMMENTS
Shaida Kate Naidoo
7200 POSTS0 COMMENTS
Ted Musemwa
7458 POSTS0 COMMENTS
Thapelo Manthata
7179 POSTS0 COMMENTS
Umr Jansen
7165 POSTS0 COMMENTS