Create a Numpy array with random values | Python

In this article, we will learn how to create a Numpy array filled with random values, given the shape and type of array.

We can use Numpy.empty() method to do this task. This method takes three parameters, discussed below –

-> shape : Number of rows
-> order : C_contiguous or F_contiguous
-> dtype : [optional, float(by Default)] Data type of returned array. 

Example #1:



filter_none

edit
close

play_arrow

link
brightness_4
code

# Python Program to create numpy array 
# filled with random values
import numpy as geek 
    
b = geek.empty(2, dtype = int
print("Matrix b : \n", b) 
    
a = geek.empty([2, 2], dtype = int
print("\nMatrix a : \n", a) 

chevron_right


Output:

Matrix b : 
 [140489599921032        21301024]

Matrix a : 
 [[140489599921048        18650592]
 [       10738656 140489568798064]]

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python Program to create numpy array 
# filled with random values
import numpy as geek 
  
# Python Program to create numpy array 
# filled with random values
import numpy as geek 
  
c = geek.empty([3, 3]) 
print("\nMatrix c : \n", c)
  
d = geek.empty([5, 3], dtype = int
print("\nMatrix d : \n", d)
   

chevron_right


Output:

Matrix c : 
 [[  1.37596097e-316   5.39314154e-317   5.39307830e-317]
 [  5.39345774e-317   5.39345774e-317   6.93325440e-310]
 [  5.39481741e-317   6.93325440e-310   8.69555537e-322]]

Matrix d : 
 [[140330665569272        23735792               0]
 [       10739936 140330589556496               0]
 [              0               0        10739904]
 [140330587337872               0        10915968]
 [              0        10739904               0]]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.