Skip to content

Instantly share code, notes, and snippets.

@hnakamur
Created July 13, 2021 06:49
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 hnakamur/40c48736f258bb18374aebc5c1637e63 to your computer and use it in GitHub Desktop.
Save hnakamur/40c48736f258bb18374aebc5c1637e63 to your computer and use it in GitHub Desktop.
Go math/rand.NewZipf example
package main
import (
"fmt"
"log"
"math/rand"
"sort"
"time"
)
func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}
func run() error {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
z := rand.NewZipf(r, 3, 10, 100)
counts := make(map[int]int)
for i := 0; i < 1000; i++ {
j := z.Uint64()
counts[int(j)]++
}
keys := make([]int, 0, len(counts))
for key := range counts {
keys = append(keys, key)
}
sort.Ints(keys)
for _, key := range keys {
fmt.Printf("%d: %d\n", key, counts[key])
}
fmt.Printf("len(keys)=%d\n", len(keys))
return nil
}
@hnakamur
Copy link
Author

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