irfansharif / cfilter

Cuckoo Filter implementation in Go, better than Bloom Filters (unmaintained)
MIT License
762 stars 37 forks source link

bug in bucket.go, rand bucket selection #10

Closed zxdev closed 7 years ago

zxdev commented 7 years ago

bucket.go, line 41 should be corrected to:

i := rand.Intn(len(b)) // - 1)

with 4 buckets rand.Intn(4) will then yield 0,1,2,3 as it is, it will only yield 0,1,2 so last bucket can never be selected. I know the go documentation says it returns 0 to n, but in the current version 1.7 it will never return n. This can easily be demonstrated with:

for {
fmt.Println("1",rand.Intn(1)) // always zero never one
fmt.Println("2",rand.Intn(2)) // always zero or one
fmt
}

https://play.golang.org/p/heTsvQLLbc

irfansharif commented 7 years ago

good catch, addressed in 983ed10.