In this article, we will learn how to use `random.sample()`

function to choose more than one item from a list, set and dictionary. The `random.sample()`

function** returns a k length list of unique elements chosen from the list, sequence or set**, used for random sampling without replacement.

- The article is part of a series
**Generate Random Data In Python.** - Also, solve our
**Python random data generation Exercise**and**Python Random data generation Quiz**to master random data generation techniques.

**Goals of this lesson **In this lesson, you’ll learn how to:

- Select more than one item from a list, set and dictionary.
- Use random sample with replacement to including repetitive items
- Get the same sampled list every time
- Generate a sample array from a large multidimensional array in Python

In simple term, we use the `random.sample()`

function of a random module to select a k length random number from a sequence or list in such a way that each number is unique. I.e., We use a `random.sample()`

function when we want to pick more than one random element from a sequence.

**For example**, `random.sample(population=[2,5,7,8,9], k=3)`

this will return `[7,5,8]`

. **Note** The `sample()`

function used for random sampling without replacement.

## The Syntax of random.sample

random.sample(population, k)

**Arguments**

`random.sample`

function has two arguments, and both are required.

- The
**population can be any sequence or set**from which you want to select a`k`

length numbers. The sequence can be String, list. you want to select from the provided population.`k`

is the number of random items`sample`

function raises a type error if you miss any of the required arguments.

**The return value from a sample function**

- It
**returns a new list containing k length elements**from the list or Sets. Remember the`random.sample()`

function doesn’t change the original list or population. - The resulting list is in selection order so that all sub-slices also be valid random samples.

Let see how to use the `random.sample()`

function to select more than one item from a list.

## random.sample to select more than one item from a list

import random print("random module's sample in Python ") list = [20,40,80,100,120] print ("choosing 3 random items from a list using sample function",random.sample(list,k=3)) list = [20,40,20,20,20] print ("choosing 3 random items from a list using sample function",random.sample(list,k=3))

**Output**: Run Online

random module's sample in Python choosing 3 random items from a list using sample function [80, 40, 100] choosing 3 random items from a list using sample function [20, 20, 20]

**Note**: As you can see we pass

`k=3`

to choose 3 random elements from a list. in return, we got a list of 3 random items. Members of the list need not be hashable or unique. It allows duplicate entries. `k`

must be less than the size of the list.**If the list contains repeats, then each occurrence is a possible selection in the sample**. i.e., `random.sample`

function can pick the repeated items from the list if the unique numbers are less than a sampling size.

## The random sample from Set

Same as the list we can select random samples without replacement out of a set. Let see this with an example program.

import random weight_set = {40, 50, 55, 65, 75,80} print ("choosing 4 random items from a set using sample method ",random.sample(weight_set, k=4))

**Output**: Run Online

choosing 4 random items from a set using sample method [40, 75, 65, 80]

## The random sample from Dictionary

Yes, it is possible to select a random key-value pair from the dictionary. as you know, `random.sample()`

function wants the population to be a sequence or set.

So you need to pass all the dictionary items to the `random.sample()`

function along with the sampling size. You can use `dict.items()`

to get all the dictionary items.

Let see this with an example.

import random marks_dict = { "Kelly": 55, "jhon": 70, "Donald": 60, "Lennin" :50 } print ("choosing 2 random items from a dictionary using sample method ",random.sample(marks_dict.items(), k=2))

**Output**: Run Online

choosing 2 random items from a dictionary using sample method [('Donald', 60), ('Kelly', 55)]

## random sample with replacement to including repetition

If you want to get a random sample with replacement, for example, you have a list of names, and you want to get random names from it, but including repetition then it also possible.

To do this, we cannot use the `random.sample()`

function. We can do that using a random.choices() function introduced in Python 3.6. Let see this with an example.

import random names = ["Roger", "Nadal", "Novac", "Andre", "Sarena", "Mariya", "Martina"] print("random sample with replacement to including repetition ", random.choices(names, k=3))

**Output**: Run Online

random sample with replacement to including repetition ['Mariya', 'Mariya', 'Andre']

**random sample without replacement**: – If you want to generate random samples without replacement out of a list or population then use

`random.sample()`

function.## The random sample from a range of integers

Generating a sample from a range of an integer is possible. You need to pass a range object to a `random.sample`

function in the place of population argument. Let see this with an example.

import random print ("choosing 4 random items from a set ",random.sample(weight_set, k=4))

Output:

choose random numbers from a range using sample method [39, 21, 41, 23, 37, 13, 35, 27]

Why use the range with a `random.sample`

function to pick random numbers? Because it is fast, memory efficient and improves the performance for sampling from a large population.

## Get the same sample list every time

It is possible to get the same list of items every time from the `random.sample`

function. We can do this by using **random.seed** and `random.sample`

function together. let see this with an example.

import random list = [20.5, 40.5, 30.5, 50.5, 70.5] random.seed(4) sample_list = random.sample(list, 3) print("random List", sample_list) random.seed(4) sample_list = random.sample(list, 3) print("random List", sample_list)

**Output**: Run Online

random List [40.5, 30.5, 20.5] random List [40.5, 30.5, 20.5]

To get the same sampled list that you want every time you need to find seed root. Refer to this article to find the seed root of your sampled list.

## Get a sample array from a large multidimensional array in Python

Most of the time we work with 2-d or 3-d arrays in Python. Let assume you want to pick more that one random rows from the multidimensional array then how to do it? Let see this with an example. In this example, We used `numpy.random.choice()`

function to pick multiple random rows from the multidimensional array.

import numpy array = numpy.array([[2 ,4, 6], [5, 10, 15], [6, 12, 18], [7, 14, 21], [8, 16, 24]]) print("Printing 2D Array") print(array) print("Choose multiple random row from 2D array") randomRows = numpy.random.randint(5, size=2) for i in randomRows: print(array[i,:])

Output:

Printing 2D Array [[ 2 4 6] [ 5 10 15] [ 6 12 18] [ 7 14 21] [ 8 16 24]] Choose multiple random row from 2D array [ 8 16 24] [2 4 6]

**Note: Above all examples are not cryptographically secure**. To cryptographically secure random output use `random.SystemRandom().sample`

instead of `random.sample()`

. Read more on how to secure random data generation in Python.

## Random’s sample() function Error and exception

A `sample`

function can raise the following two errors.

- ValueError If the sample size is larger than the population (i.e., list or set) size
- TypeError if any of the two arguments is missing.

## Next Steps

To practice what you learned in this article, I have created a Python random data generation Quiz and Exercise project.

- Solve our
**Python Random data generation Quiz**to test your random data generation concepts. - Solve
**Python Random data generation Exercise**to practice and master the random data generation techniques.

Let me know your comments and feedback in the section below.

jemima john

So I tried this code with a much larger selection of data points. Somehow it won’t execute:

PS: I’m not a statistician, so please be gently in your feedback. Thanks!

Vishal

Hi Jemina try this

masterdrew950

import numpy as np

A1, A2, A3 is the column header

0 to 4 is the index

if we print out Array1 we get:

Know you want to randomly pick three numbers from Array1:

using “random” or np.random

out put1: 2, 10, 14

“This tells you the location that the number is from” [A1,0], [A2,1], [A2,3]

So the 2 is from column A1, row 0

and the 10 is from column A2, row 1

and 14 is from column A2,row3

and if we run the code again, we get new random values

out put2: 12, 10, 8 = [A2,2], [A3,2], [A1,4]

Is it even possible pick n numbers from a 2D array and display the location they came from?

masterdrew950

How would I use the last code “Get a sample array from a large multidimensional array in Python” but instead of the whole array be able to pick one or more item from and array to create a new array with index from witch they came:

Printing 2D Array

is this even possible?

Vishal

Hey masterdrew950, Sorry I didn’t get you. I could not understand the context of your statement. can you please elaborate the issue