4 ms·
Sure it is. Base case: In a one item list, the first element is selected with probability 1. Induction: Assume we've picked an element uniformly at random fr
by dadkins 16y ago
Sure it is.
Base case: In a one item list, the first element is selected with probability 1.
Induction: Assume we've picked an element uniformly at random from the first n elements. When we encounter the n+1'th element, we pick it with probability 1/(n+1). Thus, we now have a uniformly random sample from the first n+1 elements.
- astrofinch 16y agoSorry, I misunderstood your algorithm description. I thought the word "pick" meant "return this item as your answer". I guess your intended meaning should have been clear from your code (Scala?).