# 4 Ways to Perform Random Sampling in NumPy Hello, readers! In this article, we will be focusing on 4 Easy Ways to Perform Random Sampling in Python NumPy.

So, let us get started! 🙂

Random Sampling, to give an overview, is actually selecting random values from the defined type of data and present them to be in further use.

In the course of this topic, we will be having a look at the below functions–

1. NumPy random_sample() method
2. NumPy ranf() method
3. NumPy random_integers() method
4. NumPy randint() method

## 1. NumPy random_sample() method for Random Sampling

With random_sample() method, we can sample the data values and choose random data fat ease. It selects random samples between [0.0 – 1.0] only. We can build a single sample as well as an entire array based on random values.

Have a look at the below syntax!

```random.random_sample()
```

Example:

In the below example, at first, we have performed random sampling and generated a single random value. Further, we have created a 2-dimensional array with random values by passing size as a parameter to the random_sample() function.

Note it that the random values would range between 0.0 to 1.0 only. Plus, random_sample() function generates random values of float type.

```import numpy as np

ran_val = np.random.random_sample()
print ("Random value : ", ran_val)

ran_arr = np.random.random_sample(size =(2, 4))
print ("Array filled with random float values: ", ran_arr)
```

Output:

```Random value :  0.3733413809567606
Array filled with random float values:  [[0.45421908 0.34993556 0.79641287 0.56985183]
[0.88683577 0.91995939 0.16168328 0.35923753]]
```

## 2. The random_integers() function

With random_integers() function, we can generate random values or even a multi-dimensional array of random value of type integer. That it, it generates random values of type Integer. Further, it gives us the liberty to choose the range of integer values from which the random numbers would be selected.

Syntax:

```random_integers(low, high, size)
```
• low: The lowest scale/limit for the random values to be chosen. The random values would not have a value below the low value mentioned.
• high: The highest scale/limit for the random values to be chosen. The random values would not have a value beyond the high value mentioned.
• size: The number of rows and columns for the array to be formed.

Example:

In this example, we have created a single dimensional random valued array having values between the range 5-10 only. Further, we have set up a multi-dimensional array using the same concept.

```import numpy as np

ran_val = np.random.random_integers(low = 5, high =10 , size = 3)
print ("Random value : ", ran_val)

ran_arr = np.random.random_integers(low = 5, high =10 , size = (2,4))
print ("Array filled with random float values: ", ran_arr)
```

Output:

```Random value :  [10  5  9]
Array filled with random float values:  [[ 8  8  9  6]
[ 6 10  8 10]]
```

## 3. The randint() function

The randint() function works in a similar fashion as that of random_integers() function. It creates an array having random values within the specified range of integers.

Example:

```import numpy as np

ran_val = np.random.randint(low = 5, high =10 , size = 3)
print ("Random value : ", ran_val)
```

Output:

```Random value :  [5 8 9]
```

## 4. The ranf() function

Again, ranf() function resembles random_sample() method in terms of functioning. It generates random numbers of type float between 0.0 to 1.0 only.

Example:

```import numpy as np

ran_val = np.random.ranf()
print ("Random value : ", ran_val)
```

Output:

```Random value :  0.8328458165202546
```

## Conclusion

Feel free to comment below, in case you come across any questions. For more such posts related to Python programming, Stay tuned with us! Till then, Happy Learning! 🙂