Any suggestion for random sample generation? I need to pick random 100 items from a large list every time I run it. I can do it by just comparing IDs of items and looping until count reaches a 100, but I am wondering if there are any built in features that can simplify this process.
Asked
Active
Viewed 293 times
2 Answers
5
You want random.sample(population, k)
.
Return a k length list of unique elements chosen from the population sequence. Used for random sampling without replacement.

Bill the Lizard
- 398,270
- 210
- 566
- 880
4
Use random.sample
:
import random
x = range(10000)
y = random.sample(x, 100)

nneonneo
- 171,345
- 36
- 312
- 383