The Wayback Machine - https://web.archive.org/web/20240525002953/https://www.geeksforgeeks.org/random-sampling-in-numpy-random-function/
Open In App

Random sampling in numpy | random() function

Last Updated : 26 Feb, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

numpy.random.random() 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(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 :




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


Output :

Output 1D Array filled with random floats :  [ 0.21698734  0.01617363  0.70382199]

 

Code #2 :




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


Output :

Output 2D Array filled with random floats :  [[ 0.95423066  0.35595927  0.76048569  0.90163066]
 [ 0.41903408  0.85596254  0.21666156  0.05734769]]

 
Code #3 :




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


Output :

Output 3D Array filled with random floats :  [[[ 0.07861816  0.79132387]
  [ 0.9112629   0.98162851]
  [ 0.0727613   0.03480279]]

 [[ 0.11267727  0.07631742]
  [ 0.47554553  0.83625053]
  [ 0.67781339  0.37856642]]]


Previous Article
Next Article

Similar Reads

Simple random sampling and stratified sampling in PySpark
In this article, we will discuss simple random sampling and stratified sampling in PySpark. Simple random sampling: In simple random sampling, every element is not obtained in a particular order. In other words, they are obtained randomly. That is why the elements are equally likely to be selected. In simple words, random sampling is defined as the
4 min read
Spatial Resolution (down sampling and up sampling) in image processing
A digital image is a two-dimensional array of size M x N where M is the number of rows and N is the number of columns in the array. A digital image is made up of a finite number of discrete picture elements called a pixel. The location of each pixel is given by coordinates (x, y) and the value of each pixel is given by intensity value f. Hence, the
3 min read
Random sampling in numpy | ranf() function
numpy.random.ranf() 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.ranf(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 sa
2 min read
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),
2 min read
Random sampling in numpy | sample() function
In Python numpy.random.sample() is one of the functionsgenerate that generates floating-point values in an open interval [0.0,1.0). It doesn't take up any arguments and produces a single random value each time it's called. This function is often used for statistical and simulation tasks in Python. Syntax : numpy.random.sample(size=None) Parameters:
2 min read
Random sampling in numpy | random_integers() function
numpy.random.random_integers() is one of the function for doing random sampling in numpy. It returns an array of specified shape and fills it with random integers from low (inclusive) to high (exclusive), i.e. in the interval [low, high). Syntax : numpy.random.random_integers(low, high=None, size=None) Parameters : low : [int] Lowest (signed) integ
2 min read
Random sampling in numpy | randint() function
numpy.random.randint() is one of the function for doing random sampling in numpy. It returns an array of specified shape and fills it with random integers from low (inclusive) to high (exclusive), i.e. in the interval [low, high). Syntax : numpy.random.randint(low, high=None, size=None, dtype='l') Parameters : low : [int] Lowest (signed) integer to
2 min read
5 Statistical Functions for Random Sampling in PyTorch
PyTorch is an open source machine learning library used for deep learning with more flexibility and feasibility. This is an extension of NumPy. For Statistical Functions for Random Sampling, let's see what they are along with their easy implementations. To run all these the first step is to import Pytorch by import torch. There are 5 functions: tor
6 min read
NumPy random.noncentral_f() | Get Random Samples from noncentral F distribution
The NumPy random.noncentral_f() method returns the random samples from the noncentral F distribution. Example C/C++ Code import numpy as np import matplotlib.pyplot as plt gfg = np.random.noncentral_f(1.24, 21, 3, 1000) count, bins, ignored = plt.hist(gfg, 50, density = True) plt.show() Output: Syntax Syntax: numpy.random.noncentral_f(dfnum, dfden,
1 min read
Sampling Theory
In the world of Statistics, the very first thing to be done before any estimation is to create a Sample set from the entire Population Set. The Population set can be seen as the entire tree from where data is collected whereas the Sample Set can be seen as the branch in which the actual study of observations and estimation is done. Population tree
8 min read
Practice Tags :