The Wayback Machine - https://web.archive.org/web/20210202080509/https://www.geeksforgeeks.org/random-sampling-in-numpy-random_sample-function/amp/

Random sampling in numpy | random_sample() function

numpy.random.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.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.random_sample()
print ("Output random float value : ", out_val) 
chevron_right

Output :
Output random float value :  0.9211987310893188

 

Code #2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

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

Output :
Output 2D Array filled with random floats :  [[ 0.64325146  0.4699456   0.89895437]]

 
Code #3 :

filter_none

edit
close

play_arrow

link
brightness_4
code

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

Output :
Output 3D Array filled with random floats :  [[[ 0.78245025]
  [ 0.77736746]]

 [[ 0.54389267]
  [ 0.18491758]]

 [[ 0.97428409]
  [ 0.73729256]]]

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




Article Tags :