Skip to content

Instantly share code, notes, and snippets.

@yifan-gu
Created August 23, 2014 21:24
Show Gist options
  • Save yifan-gu/b30b2a1f080b3460bd1f to your computer and use it in GitHub Desktop.
Save yifan-gu/b30b2a1f080b3460bd1f to your computer and use it in GitHub Desktop.
Shows the distribution of indgree using random picking
package main
import (
"crypto/rand"
"fmt"
"math/big"
"sync"
)
type member struct {
sync.Mutex
array []int
}
func main() {
wg := new(sync.WaitGroup)
n := 10000
fan := 5
wg.Add(n)
m := &member{array: make([]int, n)}
for i := 0; i < n; i++ {
go func() {
for j := 0; j < fan; j++ {
index, err := rand.Int(rand.Reader, big.NewInt(int64(n)))
if err != nil {
panic(err)
}
m.Lock()
m.array[int(index.Int64())]++
m.Unlock()
}
wg.Done()
}()
}
wg.Wait()
cnt := make([]int, n)
for i := 0; i < n; i++ {
cnt[m.array[i]]++
}
for i := 0; i < 50; i++ {
fmt.Println(i, ": ", cnt[i])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment