Random sampling in numpy | sample() function

numpy.random.sample() is one of the function for doing random sampling in numpy. It returns an array of specified shape and fills it with random floats in the half-open interval [0.0, 1.0).

Syntax : numpy.random.sample(size=None)

Parameters :
size : [int or tuple of ints, optional] Output shape. If the given shape is, e.g., (m, n, k), then m * n * k samples are drawn. Default is None, in which case a single value is returned.



Return : Array of random floats in the interval [0.0, 1.0). or a single such random float if size not provided.

Code #1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program explaining
# numpy.random.sample() function
  
# importing numpy
import numpy as geek
  
# output random value
out_val = geek.random.sample()
print ("Output random value : ", out_val) 

chevron_right


Output :

Output random value :  0.9261509680895836

 

Code #2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program explaining
# numpy.random.sample() function
  
# importing numpy
import numpy as geek
  
  
# output array
out_arr = geek.random.sample(size =(3, 3))
print ("Output 2D Array filled with random floats : ", out_arr) 

chevron_right


Output :

Output 2D Array filled with random floats :  [[ 0.75908777  0.88295677  0.60979136]
 [ 0.68157065  0.75100312  0.08321613]
 [ 0.8360331   0.64808891  0.14731635]]

 
Code #3 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program explaining
# numpy.random.sample() function
  
# importing numpy
import numpy as geek
  
# output array
out_arr = geek.random.sample((2, 2, 3))
print ("Output 3D Array filled with random floats : ", out_arr) 

chevron_right


Output :

Output 3D Array filled with random floats :  [[[ 0.3073475   0.75709465  0.86934712]
  [ 0.21953745  0.48138292  0.30686482]]

 [[ 0.48925625  0.60222083  0.14403257]
  [ 0.87030919  0.87298872  0.2222136 ]]]


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.