News for hackers. Stuff that matters.
Re:Algorithms Every Programmer Should Know: Reservoir Sampling by MrBuddyCasino on Friday March 06, 2015 @01:41PM (#9158665)
Thanks for the clarification, thats exactly what I was wondering about. I have two questions:

- is it a common problem to have, not knowing the set size?

- is it O(N), or rather O(n)? My intuition tells me it depends on whether or not I have to retrieve every record in the set or not, because I/O should still be much more expensive than random number generation.

Re:Algorithms Every Programmer Should Know: Reservoir Sampling by bradleyjg on Friday March 06, 2015 @02:23PM (#9158963)
Imagine you are writing software to sit on a router to:

1) collect data for some length of time, and

2) thereafter have available for querying the (maybe approximate) 25th, 50th, 95th, and 99th percentile TCP connection bandwidth thresholds, and

3) signal when a connection grows beyond the 95th percentile.

You only have a fairly limited amount of memory.

Re:Algorithms Every Programmer Should Know: Reservoir Sampling by x0x0 on Friday March 06, 2015 @10:09PM (#9160828)
While it's great for medians, reservoir sampling for calculating high or low percentiles, particularly with high dynamic range data, will probably have very high error.
Re:Algorithms Every Programmer Should Know: Reservoir Sampling by csl on Friday March 06, 2015 @01:54PM (#9158747)
> is it a common problem to have, not knowing the set size?

Yes. It could be as simple as receiving values from a Python generator (or equivalent mechanism in your favourite language), where you don't know the number of items beforehand (and querying for the length is either impossible or expensive).

In such a cases, using reservoir sampling is an elegant solution, because you're probably in a situation where you'll be iterating through all values anyway.

Re:Algorithms Every Programmer Should Know: Reservoir Sampling by bo1024 on Friday March 06, 2015 @01:48PM (#9158705)
> - is it a common problem to have, not knowing the set size?

One motivating case is when the items arrive over time, like you have a website and you want to sample uniformly from your visitors over the course of the next hour. You don't need to know how many people will arrive.