# Random sampling in numpy | sample() function

`In Python`` numpy.random.sample()` is one of the functions`generate` 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:
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.

### Example 1: Random Sampling for 1D Array

In Example 1, It prints the random float value in the range between [0.0, 1.0) in a 1D Array.

## Python3

 `# 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)`

Output:

```Output random value :  0.48333001584192203
```

### Example 2: Random Sampling for 2D Array

In Example 2, It will print a 2D array in range [0.0, 1.0) and size represents the dimensions of the array, i.e., 3,3.

## Python3

 `# 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)`

Output:

```Output 2D Array filled with random floats :  [[0.88080589 0.6975613  0.24834172]
[0.7624025  0.57821126 0.16190988]
[0.19641213 0.98098179 0.7861734 ]]
```

### Example 3: Random Sampling for 3D Array

In Example 3, it will print a 3D array in range of [0.0,1.0) and the dimensions provide for array are 2,2,3.

## Python3

 `# 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)`

Output:

```Output 3D Array filled with random floats :  [[[0.46531776 0.12490349 0.4788548 ]
[0.17803379 0.46658566 0.42292984]]
[[0.00454164 0.07650314 0.43976311]
[0.11644706 0.52697036 0.11542112]]]
```

Previous
Next