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 267 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
- 386,424
- 207
- 554
- 861
4
Use random.sample:
import random
x = range(10000)
y = random.sample(x, 100)
nneonneo
- 162,933
- 34
- 285
- 360