I want to know the use of random.sample()
method and what does it give? When should it be used and some example usage.

- 19,194
- 5
- 54
- 65

- 1,737
- 6
- 28
- 42
-
4http://docs.python.org/2/library/random.html#random.sample – sshashank124 Mar 30 '14 at 07:13
-
1http://stackoverflow.com/questions/22577916/using-nested-loops-to-generate-3-different-random-numbers/22577977#22577977 – anon582847382 Mar 30 '14 at 07:16
4 Answers
According to documentation:
random.sample(population, k)
Return a k length list of unique elements chosen from the population sequence. Used for random sampling without replacement.
Basically, it picks k unique random elements, a sample, from a sequence:
>>> import random
>>> c = list(range(0, 15))
>>> c
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
>>> random.sample(c, 5)
[9, 2, 3, 14, 11]
random.sample
works also directly from a range:
>>> c = range(0, 15)
>>> c
range(0, 15)
>>> random.sample(c, 5)
[12, 3, 6, 14, 10]
In versions earlier than 3.11, random.sample
works with sets too:
>>> c = {1, 2, 4}
>>> random.sample(c, 2)
[4, 1]
However, random.sample
doesn't work with arbitrary iterators:
>>> c = [1, 3]
>>> random.sample(iter(c), 5)
TypeError: Population must be a sequence. For dicts or sets, use sorted(d).
In version 3.9, the counts
parameter was added:
Repeated elements can be specified one at a time or with the optional keyword-only counts parameter. For example, sample(['red', 'blue'], counts=[4, 2], k=5) is equivalent to sample(['red', 'red', 'red', 'red', 'blue', 'blue'], k=5).
-
so from a given sequence it will randomly choose a given no. of elements, right?? – kartikeykant18 Mar 30 '14 at 07:16
-
-
how do we pass a column to random.sample() ? I am getting the above TypeError. – braj Aug 06 '18 at 18:51
random.sample()
also works on text
example:
> text = open("textfile.txt").read()
> random.sample(text, 5)
> ['f', 's', 'y', 'v', '\n']
\n is also seen as a character so that can also be returned
you could use random.sample()
to return random words from a text file if you first use the split method
example:
> words = text.split()
> random.sample(words, 5)
> ['the', 'and', 'a', 'her', 'of']
-
[How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) your answer is well formatted and have meaning its better to read this instruction for better answering – v8-E Jan 10 '19 at 04:43
-
1your terminology is misleading- `text` here is a string, and `words` a list- `random.sample()` works on strings and lists because they are sequences – Chris_Rands Apr 08 '19 at 12:00
random.sample(population, k)
It is used for randomly sampling a sample of length 'k'
from a population. returns a 'k'
length list of unique elements chosen from the population sequence or set
it returns a new list and leaves the original population unchanged and the resulting list is in selection order so that all sub-slices will also be valid random samples
I am putting up an example in which I am splitting a dataset randomly. It is basically a function in which you pass x_train(population)
as an argument and return indices of 60%
of the data as D_test
.
import random
def randomly_select_70_percent_of_data_from_1_to_length(x_train):
return random.sample(range(0, len(x_train)), int(0.6*len(x_train)))

- 2,663
- 1
- 11
- 31

- 549
- 1
- 6
- 14
from random import *
lst1 = sample(range(0, 1000), 100)
lst2 = sample(range(0, 1000), 100)
print(lst1)
print(lst2)
print(set(lst1).intersection(set(lst2)))
-
1This answer could _definitely_ use some supporting information. Please [edit] it to include descriptions of the code, and what it's doing. See [answer] for more information on posting good answers. – Sylvester Kruin Jan 15 '22 at 15:18