# Random Sampling with Python

Random sampling is part of the sampling technique in which each sample has an equal probability of being selected. A randomly selected sample is meant to be an unbiased representation of the total population. In this article, I’ll walk you through how we can master the art of random sampling with Python.

## Random Sampling with Python

The random module in python provides many useful functions which can be used in the task of random number generation and random sampling with Python. Now, let’s prepare a list of some numbers and import the random module and get started with the task of random sampling with Python:

Also, Read – Natural Language Processing for Other Languages with Machine Learning.

```.wp-block-code {
border: 0;
}

.wp-block-code > span {
display: block;
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
```bmi_list = [29, 18, 20, 22, 19, 25, 30, 28,22, 21, 18, 19, 20, 20, 22, 23]
import random
print("First random choice:", random.choice(bmi_list))
print("Second random choice:", random.choice(bmi_list))
print("Third random choice:", random.choice(bmi_list))```Code language: PHP (php)```
```First random choice: 29
Second random choice: 29
Third random choice: 22```

#### Choose random items from a list

The random.sample () method is useful for randomly sampling N items from a list. For example, if we want to sample N = 5 items from our BMI list, we do the following:

``print("Random sample, N = 5 :", random.sample(bmi_list, 5))`Code language: PHP (php)`
`Random sample, N = 5 : [20, 20, 18, 22, 25]`

Now let’s try sampling with 10 items:

``print("Random sample, N = 10:", random.sample(bmi_list, 10))`Code language: PHP (php)`
`Random sample, N = 10: [23, 29, 19, 18, 25, 22, 22, 20, 22, 19]`

#### Random Play of items in a List

Besides random selection and sampling, the random module also provides a function to shuffle the items in a list. Let’s print our BMI list, then print the random read result of our BMI list:

``````print("BMI list: ", bmi_list)
random.shuffle(bmi_list)
print("Shuffled BMI list: ", bmi_list)```Code language: PHP (php)```
```BMI list:  [29, 18, 20, 22, 19, 25, 30, 28, 22, 21, 18, 19, 20, 20, 22, 23]
Shuffled BMI list:  [19, 18, 19, 18, 22, 30, 23, 25, 22, 20, 28, 20, 21, 22, 29, 20]```

#### Generating Random Integers

The random module has a function to generate a random integer provided a range of values. Let’s generate a random integer between 1 and 5:

``print("Random Integer: ", random.randint(1,5))`Code language: PHP (php)`
`Random Integer: 1`

Using the randint function we can easily generate a list of random integers with the help of a for loop:

``````random_ints_list = []
for i in range(1,50):
n = random.randint(1,5)
random_ints_list.append(n)
print("My random integer list: ", random_ints_list)```Code language: PHP (php)```
`My random integer list:  [4, 4, 5, 1, 4, 2, 1, 1, 3, 1, 5, 1, 3, 3, 1, 4, 2, 3, 3, 1, 2, 4, 3, 4, 5, 1, 3, 1, 4, 5, 4, 4, 3, 3, 1, 1, 1, 1, 4, 2, 5, 5, 3, 4, 1, 2, 3, 5, 2]`

#### Calculate Evenly Distributed Numbers

The random module has a function to calculate evenly distributed numbers. For example, to generate 50 numbers evenly distributed between -10 and 1, we do the following:

``````import numpy as np
uniform_list = np.random.uniform(-10,1,50)
print("Uniformly Distributed Numbers: ", uniform_list)```Code language: JavaScript (javascript)```
```Uniformly Distributed Numbers:  [-6.90443536 -9.18626318 -5.06560151 -8.73960942  0.96557988 -3.68138829
-6.28900539 -0.87698435 -3.56088964 -9.58036607 -7.05472359 -8.99350997
-5.49612048 -1.35906463 -4.88964676 -9.57245988 -6.19384559 -9.54775125
-4.80270833  0.77425365 -7.21144701 -2.68992034 -9.49240533 -8.82326373
-8.38296649 -3.62650673 -6.72408703 -7.67296498 -5.79420671 -5.74316464
0.84972621 -0.66996594  0.44308895 -7.24030997 -4.90555622 -4.99818729
-9.64282377 -7.07056048 -1.35891742  0.90118395 -8.96232994 -4.95434609
-5.87886278 -4.8805922  -4.05263321 -8.30581103 -6.52358278 -2.6470581
0.26930296 -3.1271868 ]```

Also, Read – Analyze Healthcare Data with Python.

You can explore more on this topic by manipulating with the figures. I hope you liked this article on randomly sampling data with Python. Feel free to ask your valuable questions in the comments section below. You can also follow me on Medium to learn every topic of Python and Machine Learning. 