Skip to content

Commit

Permalink
Add false positives example
Browse files Browse the repository at this point in the history
  • Loading branch information
korthaj committed May 7, 2017
1 parent d68badd commit 08eaa09
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package bloom_test
import (
"fmt"
"github.com/yourbasic/bloom"
"math/rand"
"strconv"
)

// Create and use a Bloom filter.
func Example() {
func Example_basics() {
// Create a Bloom filter with room for 10000 elements
// at a false-positives rate less than 0.5 percent.
blacklist := bloom.New(10000, 200)
Expand All @@ -23,3 +25,28 @@ func Example() {
}
// Output: https://rascal.com seems to be shady.
}

// Count the number of false positives.
func Example_falsePositives() {
// Create a Bloom filter with room for n elements
// at a false-positives rate less than 1/p.
n := 1000
p := 100
filter := bloom.New(n, p)

// Add n random strings.
for i := 0; i < n; i++ {
filter.Add(strconv.FormatUint(rand.Uint64(), 10))
}

// Do n random lookups and count the (mostly accidental) hits.
// It shouldn't be much more than n/p, and hopefully less.
count := 0
for i := 0; i < n; i++ {
if filter.Test(strconv.FormatUint(rand.Uint64(), 10)) {
count++
}
}
fmt.Println(count, "mistakes were made.")
// Output: 1 mistakes were made.
}

0 comments on commit 08eaa09

Please sign in to comment.