本文整理汇总了Golang中math/rand.NewZipf函数的典型用法代码示例。如果您正苦于以下问题:Golang NewZipf函数的具体用法?Golang NewZipf怎么用?Golang NewZipf使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewZipf函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ZipfRandom
// ZipfRandom emits a Zipfian distributed random number
// notation follows the wikipedia page http://en.wikipedia.org/wiki/Zipf%E2%80%93Mandelbrot_law not the golang Zipf parameters
func ZipfRandom() Spec {
return Spec{
Name: "Zipf",
Inputs: []Pin{
Pin{"q", NUMBER}, Pin{"s", NUMBER}, Pin{"N", NUMBER}},
Outputs: []Pin{Pin{"draw", NUMBER}},
Kernel: func(in, out, internal MessageMap, ss Source, i chan Interrupt) Interrupt {
q, ok := in[0].(float64)
if !ok {
out[0] = NewError("q must be a number")
return nil
}
s, ok := in[1].(float64)
if !ok {
out[0] = NewError("s must be a number")
return nil
}
N, ok := in[2].(float64)
if !ok {
out[0] = NewError("N must be an number")
return nil
}
z := rand.NewZipf(RAND, s, q, uint64(N))
out[0] = z.Uint64()
return nil
},
}
}
示例2: Emit
func (d Zipf) Emit() (interface{}, error) {
if d.zipf == nil {
r := rand.New(rand.NewSource(42))
d.zipf = rand.NewZipf(r, d.S, d.V, d.Imax)
}
return d.zipf.Uint64(), nil
}
示例3: newZipfValues
func newZipfValues() *zipfValues {
r := rand.New(rand.NewSource(rngSeed))
z := rand.NewZipf(r, 1.2, 1, 1024*1024)
return &zipfValues{
z: z,
}
}
示例4: TestSWilk
func TestSWilk(t *testing.T) {
const l = 1000
r := rand.New(rand.NewSource(1))
zr := rand.NewZipf(r, 1.01, 1, 10000)
zipf := make([]float64, l)
for i := 0; i < l; i++ {
zipf[i] = float64(zr.Uint64())
}
var w, pw float64
var err error
w, pw, err = SWilk(zipf)
t.Logf("zipf: w=%f pw=%f err=%v", w, pw, err)
// fly wing lengths in mm are normally distributed
// via http://www.seattlecentral.edu/qelp/sets/057/057.html
var wings = []float64{
43, 48, 45, 48, 45, 39, 47, 43, 37, 46, 38, 47, 53, 43, 42, 44,
51, 42, 48, 42, 36, 46, 44, 41, 50, 47, 47, 44, 45, 46, 46, 40,
49, 40, 42, 45, 41, 51, 45, 44, 38, 50, 51, 41, 46, 49, 48, 47,
40, 42, 44, 45, 47, 42, 45, 46, 47, 42, 46, 47, 39, 45, 40, 50,
49, 52, 48, 45, 45, 54, 50, 41, 46, 48, 43, 43, 53, 41, 51, 46,
41, 48, 43, 47, 43, 48, 43, 44, 50, 44, 52, 49, 44, 46, 55, 50,
49, 44, 49, 49,
}
w, pw, err = SWilk(wings)
t.Logf("wings: w=%f pw=%f err=%v", w, pw, err)
}
示例5: Run
// Run is the block's main loop. Here we listen on the different channels we set up.
// this is actually the Zipf-Manadlebrot "law".
// http://en.wikipedia.org/wiki/Zipf%E2%80%93Mandelbrot_law
// the parameter `v` is denoted `q` on wikipedia.
func (b *Zipf) Run() {
var err error
var s, v, imax float64
s = 2.0
v = 5.0
imax = 99.0
r := rand.New(rand.NewSource(12345))
sampler := rand.NewZipf(r, s, v, uint64(imax))
for {
select {
case ruleI := <-b.inrule:
// set a parameter of the block
rule, ok := ruleI.(map[string]interface{})
if !ok {
b.Error(errors.New("couldn't assert rule to map"))
}
s, err = util.ParseFloat(rule, "s")
if err != nil {
b.Error(err)
}
v, err = util.ParseFloat(rule, "v")
if err != nil {
b.Error(err)
}
imax, err = util.ParseFloat(rule, "N")
if err != nil {
b.Error(err)
}
sampler = rand.NewZipf(r, s, v, uint64(imax))
case <-b.quit:
// quit the block
return
case <-b.inpoll:
// deal with a poll request
b.out <- map[string]interface{}{
"sample": float64(sampler.Uint64()),
}
case c := <-b.queryrule:
// deal with a query request
c <- map[string]interface{}{
"s": s,
"v": v,
"N": imax,
}
}
}
}
示例6: runBenchmarkInsertDistinct
// Benchmark inserting distinct rows in batches where the min and max rows in
// separate batches overlap. This stresses the command queue implementation and
// verifies that we're allowing parallel execution of commands where possible.
func runBenchmarkInsertDistinct(b *testing.B, db *gosql.DB, numUsers int) {
if _, err := db.Exec(`DROP TABLE IF EXISTS bench.insert_distinct`); err != nil {
b.Fatal(err)
}
const schema = `
CREATE TABLE bench.insert_distinct (
articleID INT,
userID INT,
uniqueID INT DEFAULT unique_rowid(),
PRIMARY KEY (articleID, userID, uniqueID))
`
if _, err := db.Exec(schema); err != nil {
b.Fatal(err)
}
b.ResetTimer()
var wg sync.WaitGroup
wg.Add(numUsers)
var count int64
for i := 0; i < numUsers; i++ {
go func(i int) {
defer wg.Done()
var buf bytes.Buffer
rnd := rand.New(rand.NewSource(int64(i)))
// Article IDs are chosen from a zipf distribution. These values select
// articleIDs that are mostly <10000. The parameters were experimentally
// determined, but somewhat arbitrary.
zipf := rand.NewZipf(rnd, 2, 10000, 100000)
for {
n := atomic.AddInt64(&count, 1)
if int(n) >= b.N {
return
}
// Insert between [1,100] articles in a batch.
numArticles := 1 + rnd.Intn(100)
buf.Reset()
buf.WriteString(`INSERT INTO bench.insert_distinct VALUES `)
for j := 0; j < numArticles; j++ {
if j > 0 {
buf.WriteString(", ")
}
fmt.Fprintf(&buf, "(%d, %d)", zipf.Uint64(), n)
}
if _, err := db.Exec(buf.String()); err != nil {
b.Fatal(err)
}
}
}(i)
}
wg.Wait()
b.StopTimer()
}
示例7: fuzzyInt
func (f *protoFuzzer) fuzzyInt() int64 {
i := int64(rand.NewZipf(f.r, 3, 1, 200).Uint64() + 1)
if rand.Intn(2) == 0 {
i = -i
}
fmt.Printf("Changing int by %d\n", i)
return i
}
示例8: makeZipfer
//makeZipfer: Initialize the stream of random elements for the tests.
func makeZipfer(r *rand.Rand) *rand.Zipf {
//Make the zipf distribution of random input
var s, v float64
var imax uint64
s = 1.2
v = 1.0
imax = 2 << 10
zipfer := rand.NewZipf(r, s, v, imax)
return zipfer
}
示例9: NewZipfKey
// Index of partition starts from 0
// Integer Key starts from 0 also
func NewZipfKey(partIndex int, nKeys int64, nParts int, pKeysArray []int64, s float64, hp *HashPartitioner) *ZipfKey {
zk := &ZipfKey{
partIndex: partIndex,
nParts: nParts,
nKeys: nKeys,
pKeysArray: pKeysArray,
hp: hp,
}
zk.isPartition = (*SysType == PARTITION) || *PhyPart
zk.wholeUniform = rand.New(rand.NewSource(time.Now().Unix() / int64(partIndex+1)))
if zk.isPartition {
zk.partUniform = make([]*rand.Rand, nParts)
for i := 0; i < nParts; i++ {
zk.partUniform[i] = rand.New(rand.NewSource(time.Now().Unix() / int64(partIndex*13+i*7+1)))
}
}
// Uniform distribution
if s == 1 {
zk.isZipf = false
} else {
zk.isZipf = true
// Generate Zipf for whole store
zk.wholeZipf = rand.NewZipf(zk.wholeUniform, s, 1, uint64(nKeys-1))
if zk.isPartition {
// Generate Zipf for for each part
zk.partZipf = make([]*rand.Zipf, nParts)
for i := 0; i < nParts; i++ {
zk.partZipf[i] = rand.NewZipf(zk.partUniform[i], s, 1, uint64(pKeysArray[i]-1))
}
}
}
return zk
}
示例10: main
func main() {
//Handling command line parameters
log.Printf("starting main\n")
src := rand.NewSource(0)
r := rand.New(src)
var Depth, Width, efactor, numElements int64
Depth = *depthPtr
Width = *widthPtr
efactor = *efactorPtr
numElements = Depth * Width * efactor
log.Printf("params:Depth:%d\n", Depth)
log.Printf("params:Width:%d\n", Width)
log.Printf("params:efactor:%d\n", efactor)
log.Printf("params:numElements:%d\n", numElements)
//Initialize Data Structures
hslice := RandomHashes(r, Depth)
cms := NewCMSketch(Depth, Width)
cms.Hash = hslice
//Make the zipf distribution of random input
var j, z int64
var s, v float64
var imax uint64
s = 1.2
v = 1.0
imax = 2 << 10
zipfer := rand.NewZipf(r, s, v, imax)
//Use set to store the exact answers
set := make(map[int64]int64)
log.Printf("Inserting\n")
ts := time.Now()
for j = 0; j < numElements; j++ {
z = int64(zipfer.Uint64())
//set[z] += 1
//fmt.Println(z)
cms.UpdateSerial(z, 1)
}
te := time.Now().Sub(ts)
fmt.Printf("time: %v\n", te)
fmt.Printf("%s\n", cms.Counter.String())
var qj int64 //approximate answers
var totalLoss float64
loss := func(cj, qj float64) float64 {
return (cj - qj) * (cj - qj)
}
for j, cj := range set {
qj = cms.PointQuery(j)
//fmt.Printf("results:%d %d %d %f\n", j, qj, cj, float64(qj)/float64(cj))
totalLoss += loss(float64(cj), float64(qj))
}
fmt.Printf("Total Loss: %f/%d\n", totalLoss, numElements)
}
示例11: New
func New(seed int64, n int) func() int {
src := rand.NewSource(seed)
r := rand.New(src)
z := rand.NewZipf(r, 2, 1, uint64(n-1))
c := make(chan int, 32)
go func() {
for {
c <- int(z.Uint64())
}
}()
return func() int {
return <-c
}
}
示例12: BenchmarkCounter
func BenchmarkCounter(b *testing.B) {
once.Do(func() {
var buf []byte
buf, err = ioutil.ReadFile("/usr/share/dict/connectives")
if err != nil {
return
}
for _, b := range bytes.Fields(buf) {
words = append(words, string(b))
}
})
if err != nil {
b.Skipf("could not open dictionary: %v", err)
}
c := NewCounter()
r := rand.New(rand.NewSource(1234))
z := rand.NewZipf(r, 2, 1, uint64(len(words)-1))
var seq [1024]int
for i := 0; i < len(seq); i++ {
// seq[i] = rand.Intn(len(words))
seq[i] = int(z.Uint64())
}
_ = z
b.ResetTimer()
// for i := 0; i < b.N; i++ {
// c.Add(words[b.N%len(words)])
// c.Add(words[rand.Intn(len(words))])
// }
b.RunParallel(func(pb *testing.PB) {
var i int
for pb.Next() {
// c.Add(words[rand.Intn(len(words))])
// c.Add(words[int(z.Uint64())])
c.Add(words[seq[i]])
i = (i + 1) % 1024
}
})
b.StopTimer()
if got := c.Sum(); got != b.N {
b.Errorf("Sum=%d want %d", got, b.N)
}
}
示例13: benchmarkObserve
func benchmarkObserve(b *testing.B, capacity int, distinct uint64) {
r := rand.New(rand.NewSource(1))
zipf := rand.NewZipf(r, 1.5, 5, distinct)
items := make(chan string, b.N)
for i := 0; i < b.N; i++ {
items <- strconv.FormatUint(zipf.Uint64(), 10)
}
summary := NewSummary(capacity)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
summary.Observe(<-items)
}
})
}
示例14: main
func main() {
words := data.GetData()
// shuffle
rand.Seed(time.Now().UnixNano())
for i := range words {
j := rand.Intn(i + 1)
words[i], words[j] = words[j], words[i]
}
client, err := skizze.Dial("127.0.0.1:3596", skizze.Options{Insecure: true})
if err != nil {
fmt.Printf("Error connecting to Skizze: %s\n", err)
return
}
domainName := "skizze_stress"
if _, err := client.CreateDomain(domainName); err != nil {
fmt.Println(err)
}
end := time.Duration(0)
r := rand.New(rand.NewSource(time.Now().UnixNano()))
zipf := rand.NewZipf(r, 1.1, 1.1, uint64(len(words)-1))
totalAdds := 0
for i := 0; i < 100000; i++ {
fill := make([]string, 1000, 1000)
for j := 0; j < len(fill); j++ {
k := zipf.Uint64()
fill[j] = words[k]
}
totalAdds += len(fill)
t := time.Now()
if err := client.AddToDomain(domainName, fill...); err != nil {
fmt.Println(err)
return
}
end += time.Since(t)
if end.Seconds() > 0 {
fmt.Printf("Added %d values (%d unique) in %ds (avg. %d v/s)\n", totalAdds, len(words), int(end.Seconds()), totalAdds/int(end.Seconds()+1))
}
}
client.Close()
fmt.Printf("Added %d values (%d unique) in %ds (avg. %d v/s)\n", totalAdds, len(words), int(end.Seconds()), totalAdds/int(end.Seconds()+1))
}
示例15: TestGenZipfGo
func TestGenZipfGo(t *testing.T) {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
z := rand.NewZipf(r, 1.000001, 1, uint64(100))
n := 1000
x := make([]uint64, n)
for i := 0; i < n; i++ {
x[i] = z.Uint64()
}
first := 0
second := 0
third := 0
for i := 0; i < len(x); i++ {
if x[i] == uint64(0) {
first++
} else if x[i] == uint64(1) {
second++
} else if x[i] == 2 {
third++
}
}
fmt.Printf("go 1.000001: first: %v, second: %v, third: %v\n", first, second, third)
}