Skip to content

Instantly share code, notes, and snippets.

@chrisshroba
Created March 7, 2016 01:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisshroba/178548573b914cc1f8b4 to your computer and use it in GitHub Desktop.
Save chrisshroba/178548573b914cc1f8b4 to your computer and use it in GitHub Desktop.
473 Homework 5 #1a Pseudocode
GetKSamples(stream S, int k):
samples = list of length k // samples is the list we'll return
for i gets 1...k:
samples[i] = next item in S // Add the first five items in the
// stream to samples
l <- k // Count of things seen so far
while S is not done
x <- next item in S
l <- l+1 // Increment count
if Random(l) <= k // With 5/l probability...
samples[Random(k)] = x // Replace a random item with x
return samples
@chrisshroba
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment