Created
July 13, 2021 06:49
-
-
Save hnakamur/40c48736f258bb18374aebc5c1637e63 to your computer and use it in GitHub Desktop.
Go math/rand.NewZipf example
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
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://play.golang.org/p/dT7vXT_Vjc0