I have a sorted list, let say: (its not really just numbers, its a list of objects that are sorted with a complicated time consuming algorithm)
mylist = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ,9 , 10 ]
Is there some python function that will give me N of the items, but will keep the order?
Example:
randomList = getRandom(mylist,4)
# randomList = [ 3 , 6 ,7 , 9 ]
randomList = getRandom(mylist,4)
# randomList = [ 1 , 2 , 4 , 8 ]
etc...
Following code will generate a random sample of size 4:
(note: with Python 2, better use
xrange
instead ofrange
)Explanation
generates a random sample of the indices of the original list.
These indices then get sorted to preserve the ordering of elements in the original list.
Finally, the list comprehension pulls out the actual elements from the original list, given the sampled indices.
Maybe you can just generate the sample of indices and then collect the items from your list.
Simple-to-code O(N + K*log(K)) way
Take a random sample without replacement of the indices, sort the indices, and take them from the original.
Or more concisely:
Optimized O(N)-time, O(1)-auxiliary-space way
You can alternatively use a math trick and iteratively go through
myList
from left to right, picking numbers with dynamically-changing probability(N-numbersPicked)/(total-numbersVisited)
. The advantage of this approach is that it's anO(N)
algorithm since it doesn't involve sorting!Proof of concept and test that probabilities are correct:
Simulated with 1 trillion pseudorandom samples over the course of 5 hours:
Probabilities diverge from true probabilities by less a factor of 1.0001. Running this test again resulted in a different order meaning it isn't biased towards one ordering. Running the test with fewer samples for
[0,1,2,3,4], k=3
and[0,1,2,3,4,5], k=4
had similar results.edit: Not sure why people are voting up wrong comments or afraid to upvote... NO, there is nothing wrong with this method. =)
(Also a useful note from user tegan in the comments: If this is python2, you will want to use xrange, as usual, if you really care about extra space.)
edit: Proof: Considering the uniform distribution (without replacement) of picking a subset of
k
out of a populationseq
of sizelen(seq)
, we can consider a partition at an arbitrary pointi
into 'left' (0,1,...,i-1) and 'right' (i,i+1,...,len(seq)). Given that we pickednumbersPicked
from the left known subset, the remaining must come from the same uniform distribution on the right unknown subset, though the parameters are now different. In particular, the probability thatseq[i]
contains a chosen element is#remainingToChoose/#remainingToChooseFrom
, or(k-numbersPicked)/(len(seq)-i)
, so we simulate that and recurse on the result. (This must terminate since if #remainingToChoose == #remainingToChooseFrom, then all remaining probabilities are 1.) This is similar to a probability tree that happens to be dynamically generated. Basically you can simulate a uniform probability distribution by conditioning on prior choices (as you grow the probability tree, you pick the probability of the current branch such that it is aposteriori the same as prior leaves, i.e. conditioned on prior choices; this will work because this probability is uniformly exactly N/k).edit: Timothy Shields mentions Reservoir Sampling, which is the generalization of this method when
len(seq)
is unknown (such as with a generator expression). Specifically the one noted as "algorithm R" is O(N) and O(1) space if done in-place; it involves taking the first N element and slowly replacing them (a hint at an inductive proof is also given). There are also useful distributed variants and miscellaneous variants of reservoir sampling to be found on the wikipedia page.edit: Here's another way to code it below in a more semantically obvious manner.
)
random.sample implement it.
Apparently
random.sample
was introduced in python 2.3so for version under that, we can use shuffle (example for 4 items):