0

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.

rodling
  • 988
  • 5
  • 18
  • 44
  • I posted a solution for a similar problem (sample size unknown in advance), but it actually is a random sample generator and can be a solution to your question too: http://stackoverflow.com/a/30063866/694360 – mmj May 05 '15 at 22:17

2 Answers2

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