Last active
October 9, 2024 07:25
-
-
Save nikolaydubina/02e6293b00bef444fad332a280324923 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// πππ₯πππ₯πππ₯πππ₯πππ₯πππ₯ | |
// time: O(N) | |
// space: O(1) | |
// why: making smoothie! π§ | |
package main | |
import ( | |
"fmt" | |
"math/rand" | |
) | |
type Node struct { | |
V string | |
Next *Node | |
} | |
func sampleUniform(l *Node) *Node { | |
if l == nil { | |
return nil | |
} | |
r := l | |
count := 0 | |
for q := l; q != nil; q = q.Next { | |
count += 1 | |
if rand.Float64() < 1/float64(count) { | |
r = q | |
} | |
} | |
return r | |
} | |
func main() { | |
l := &Node{V: "π₯", Next: &Node{V: "π", Next: &Node{V: "π"}}} | |
count := make(map[string]int) | |
for range 1000 { | |
count[sampleUniform(l).V]++ | |
} | |
fmt.Print(count) | |
// Output: map[π:336 π:340 π₯:324] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
for truly uniform sampling probability for each is 1/N. but when N -> infinity, then probability of picking any is approaching 0. so algorithms probability has to approach 0 too. meaning it will underfloat float value and will never trigger. so basically you should never select any value and keep iterating.
now, this is different from non-infinite but undetermined N. in which case probability is non zero, but unknown and depends on number of elements. we basically have to go through whole sequence. we have to decrease probability of picking last one as we go.