func (fg FlowGenerator) makeFlows(logger chan LogEvent) EventQueue { lambda := (fg.bandwidth * 1e9 * fg.load) / (fg.cdf.meanFlowSize() * 1500 * 8) lambda /= 143 creationQueue := make(EventQueue, 0) defer func() { creationQueue = nil }() heap.Init(&creationQueue) for i := 0; i < NUM_HOSTS; i++ { for j := 0; j < NUM_HOSTS; j++ { if i == j { continue } f := &Flow{Start: 1e6 + (rand.ExpFloat64()/lambda)*1e6, Size: fg.cdf.value(), Source: uint8(i), Dest: uint8(j), LastTime: 0, FinishEvent: nil} heap.Push(&creationQueue, makeCreationEvent(f)) } } eventQueue := make(EventQueue, 0) for uint(len(eventQueue)) < fg.numFlows { ev := heap.Pop(&creationQueue).(*Event) logger <- LogEvent{Time: 0, Type: LOG_FLOW_GEN, Flow: ev.Flow} eventQueue = append(eventQueue, makeArrivalEvent(ev.Flow)) nextTime := ev.Time + (rand.ExpFloat64()/lambda)*1e6 f := &Flow{Start: nextTime, Size: fg.cdf.value(), Source: ev.Flow.Source, Dest: ev.Flow.Dest, LastTime: 0, FinishEvent: nil} heap.Push(&creationQueue, makeCreationEvent(f)) } return eventQueue }
func BenchmarkReadFloat(b *testing.B) { b.StopTimer() c := new(Context) rand.Seed(time.Now().UnixNano()) max := 512 floats := make([]*bytes.Buffer, max) for i := 0; i < max; i++ { w := new(bytes.Buffer) v := rand.ExpFloat64() - rand.ExpFloat64() w.Write([]byte{ettNewFloat}) binary.Write(w, binary.BigEndian, math.Float64bits(v)) floats[i] = w } b.StartTimer() for i := 0; i < b.N; i++ { in := floats[i%max] _, err := c.Read(in) if err != io.EOF && err != nil { b.Fatal(err) } } }
func s(numTrains int, safetyTime int, rideTime int, minTransTime int, avgTransTime int) []int { d := make([]int, 0) trains := make([]int, numTrains) c := 0 avg := float64(avgTransTime - minTransTime) t := 0 dep := minTransTime + int(rand.ExpFloat64()*avg) t = dep trains[c] = dep + rideTime c = (c + 1) % numTrains d = append(d, dep) for t < 43200 { //in seconds if trains[c] > t { t = trains[c] } t += minTransTime + int(rand.ExpFloat64()*avg) if t < dep+safetyTime { t = dep + safetyTime } dep = t d = append(d, dep) } fmt.Println(d) return d }
func TestCompressionRatio(t *testing.T) { r := rand.New(rand.NewSource(0)) mean := 0.0 count := 100 for i := 0; i < count; i++ { length := r.Intn(5000) + 100 c := NewCompressionBuffer() data := []float64{} value := rand.ExpFloat64() * 1e-5 for j := 0; j < length; j++ { if rand.Intn(10) != 0 { value += rand.ExpFloat64() } data = append(data, value) } c.Compress(data) c.Finalize() compressed := c.Bytes() compressionRatio := float64(length*8) / float64(len(compressed)) if compressionRatio < 1 { t.Errorf("Data size was increased when compressed") t.Errorf("Compression ratio: %f; original %d: compressed %d", float64(length*8)/float64(len(compressed)), length*8, len(compressed)) } mean += compressionRatio } mean = mean / float64(count) t.Logf("mean compression ratio: %f", mean) }
func singleServer() { for i := 0; i < 1000; i++ { fmt.Println("NormFloat64", rand.NormFloat64(), "Int63n", rand.Int63n(100), "ExpFloat64", rand.ExpFloat64()) } }
// Evolve implements the inner loop of the evolutionary algorithm. // The population calls the Evolve method of each genome, in parallel. Then, // each receiver returns a value to replace it in the next generation. func (q *queens) Evolve(matingPool ...evo.Genome) evo.Genome { // Crossover: // We're implementing a diffusion model. For each member of the population, // we receive a small mating pool containing only our neighbors. We choose // a mate using a random binary tournament and create a child with // partially mapped crossover. mate := sel.BinaryTournament(matingPool...).(*queens) child := &queens{gene: pool.Get().([]int)} perm.PMX(child.gene, q.gene, mate.gene) // Mutation: // Perform n random swaps where n is taken from an exponential distribution. mutationCount := math.Ceil(rand.ExpFloat64() - 0.5) for i := float64(0); i < mutationCount; i++ { j := rand.Intn(len(child.gene)) k := rand.Intn(len(child.gene)) child.gene[j], child.gene[k] = child.gene[k], child.gene[j] } // Replacement: // Only replace if the child is better or equal. if q.Fitness() > child.Fitness() { return q } return child }
func randomSlice(n int) []float64 { slice := make([]float64, n) for i := range slice { slice[i] = rand.ExpFloat64() } return slice }
func gaussianNoise(data []float64) []float64 { result := make([]float64, len(data)) for i := range data { result[i] = data[i] + rand.ExpFloat64() } return result }
func main() { end = make(map[int64]int) b := big.NewInt(int64(10)) for i := 0; i < size; i++ { r, _ := c_rand.Int(c_rand.Reader, b) m := r.Int64() end[m]++ } for i, j := range end { fmt.Print(i) for m := 0; m < j/(size/100); m++ { fmt.Print("*") } fmt.Println(" ", j) } for i := 0; i < 11; i++ { fmt.Println(c_rand.Prime(c_rand.Reader, 64)) } for i := 0; i < 11; i++ { fmt.Println(m_rand.ExpFloat64()) } }
// Generate and send a random GetValuesSingle request. func (l *Load) sendMulti(client *gen.HFileServiceClient, diff *gen.HFileServiceClient) { numKeys := int(math.Abs(rand.ExpFloat64()*10) + 1) keys := l.randomKeys(numKeys) r := &gen.SingleHFileKeyRequest{HfileName: &l.collection, SortedKeys: keys} before := time.Now() resp, err := client.GetValuesMulti(r) if err != nil { log.Println("[GetValuesMulti] Error fetching value:", err, util.PrettyKeys(keys)) } report.TimeSince(l.rtt+".overall", before) report.TimeSince(l.rtt+".getValuesMulti", before) if diff != nil { beforeDiff := time.Now() diffResp, diffErr := diff.GetValuesMulti(r) if diffErr != nil { log.Println("[GetValuesMulti] Error fetching diff value:", diffErr, util.PrettyKeys(keys)) } report.TimeSince(l.diffRtt+".overall", beforeDiff) report.TimeSince(l.diffRtt+".getValuesMulti", beforeDiff) if err == nil && diffErr == nil && !reflect.DeepEqual(resp, diffResp) { report.Inc("diffs") report.Inc("diffs.getValuesMulti") log.Printf("[DIFF-getValuesMulti] req: %v\n \torig: %v\n\n\tdiff: %v\n", r, resp, diffResp) } } }
// Generate and send a random GetValuesSingle request. func (l *Load) sendPrefixes(client *gen.HFileServiceClient, diff *gen.HFileServiceClient) { numKeys := int(math.Abs(rand.ExpFloat64()*10) + 1) fullKeys := l.randomKeys(numKeys) prefixes := make([][]byte, len(fullKeys)) for i, v := range fullKeys { prefixes[i] = v[:len(v)-2] } sort.Sort(util.Keys(prefixes)) r := &gen.PrefixRequest{HfileName: &l.collection, SortedKeys: prefixes} before := time.Now() resp, err := client.GetValuesForPrefixes(r) if err != nil { log.Println("[GetValuesForPrefixes] Error fetching value:", err, util.PrettyKeys(prefixes)) } report.TimeSince(l.rtt+".overall", before) report.TimeSince(l.rtt+".GetValuesForPrefixes", before) if diff != nil { beforeDiff := time.Now() diffResp, diffErr := diff.GetValuesForPrefixes(r) if diffErr != nil { log.Println("[GetValuesForPrefixes] Error fetching diff value:", diffErr, util.PrettyKeys(prefixes)) } report.TimeSince(l.diffRtt+".overall", beforeDiff) report.TimeSince(l.diffRtt+".GetValuesForPrefixes", beforeDiff) if err == nil && diffErr == nil && !reflect.DeepEqual(resp, diffResp) { report.Inc("diffs") report.Inc("diffs.GetValuesForPrefixes") log.Printf("[DIFF-GetValuesForPrefixes] req: %v\n \torig: %v\n\n\tdiff: %v\n", r, resp, diffResp) } } }
func main() { rand.Seed(time.Now().UnixNano()) // taking "set" literally from task description s := map[float64]bool{} // pick number of elements to add to set n := rand.Intn(11) // add random numbers, also throw in an occasional NaN or Inf. for i := 0; i < n; i++ { switch rand.Intn(10) { case 0: s[math.NaN()] = true case 1: s[math.Inf(1)] = true default: s[rand.ExpFloat64()] = true } } fmt.Print("s:") for e := range s { fmt.Print(" ", e) } fmt.Println() switch lg, ok, nan := largest(s); { case ok && !nan: fmt.Println("largest:", lg) case ok: fmt.Println("largest:", lg, "(NaN present in data)") case nan: fmt.Println("no largest, all data NaN") default: fmt.Println("no largest, empty set") } }
// Generates sample from general Levy-stable distibution. func Sample(alpha float64, beta float64, mu float64, sigma float64) float64 { if beta == 0.0 { return symmetric(alpha, mu, sigma) } v := math.Pi * (rand.Float64() - 0.5) w := 0.0 for w == 0.0 { w = rand.ExpFloat64() } if alpha == 1.0 { x := ((0.5*math.Pi+beta*v)*math.Tan(v) - beta*math.Log(0.5*math.Pi*w*math.Cos(v)/(0.5*math.Pi+beta*v))) / (0.5 * math.Pi) return sigma*x + beta*sigma*math.Log(sigma)/(0.5*math.Pi) + mu } t := beta * math.Tan(0.5*math.Pi*alpha) s := math.Pow(1.0+t*t, 1.0/(2.0*alpha)) b := math.Atan(t) / alpha x := s * math.Sin(alpha*(v+b)) * math.Pow(math.Cos(v-alpha*(v+b))/w, (1.0-alpha)/alpha) / math.Pow(math.Cos(v), 1.0/alpha) return sigma*x + mu }
// Helper function, makes pseudo-random slices. func makeRandSlice(length uint) (randslice []float64) { randslice = make([]float64, length) for i := range randslice { randslice[i] = rand.ExpFloat64() } return }
// Creates pseudo-random vectors, does scalar multiplication with pseudo-random // floats, then checks if the result is correct. It checks both the in-place // and the non-destructive version. func TestScale(t *testing.T) { var i, j uint for i = 0; i < 100; i++ { a := makeRandomVector(i) x := rand.ExpFloat64() b := Scale(a, x) for j = 0; j < i; j++ { if b.dims[j] != a.dims[j]*x { t.Error("Scalar Multiplication failed, ", "didn't get expected values.") t.Logf("%f * %f != %f", a.dims[j], x, b.dims[j]) } } // Test in-place scalar multiplication b = a.Copy() b.Scale(x) for j = 0; j < i; j++ { if b.dims[j] != a.dims[j]*x { t.Error("In-place Scalar Multiplication failed, ", "didn't get expected values.") t.Logf("%f * %f != %f", a.dims[j], x, b.dims[j]) } } } }
func init() { for i := 0; i < len(DataZeroFloats); i++ { DataZeroFloats[i] = point{float64(0), uint32(i * 60)} } for i := 0; i < len(DataSameFloats); i++ { DataSameFloats[i] = point{float64(1234.567), uint32(i * 60)} } for i := 0; i < len(DataSmallRangePosInts); i++ { DataSmallRangePosInts[i] = point{TwoHoursData[i%120].v, uint32(i * 60)} } for i := 0; i < len(DataSmallRangePosFloats); i++ { v := float64(TwoHoursData[i%120].v) * 1.2 DataSmallRangePosFloats[i] = point{v, uint32(i * 60)} } for i := 0; i < len(DataLargeRangePosFloats); i++ { v := (float64(TwoHoursData[i%120].v) / 1000) * math.MaxFloat64 DataLargeRangePosFloats[i] = point{v, uint32(i * 60)} } for i := 0; i < len(DataRandomPosFloats); i++ { DataRandomPosFloats[i] = point{rand.ExpFloat64(), uint32(i * 60)} } for i := 0; i < len(DataRandomFloats); i++ { DataRandomFloats[i] = point{rand.NormFloat64(), uint32(i * 60)} } }
func (b *Exponential) Run() { var err error λ := 1.0 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")) } λ, err = util.ParseFloat(rule, "rate") if err != nil { b.Error(err) } case <-b.quit: // quit the block return case <-b.inpoll: // deal with a poll request b.out <- map[string]interface{}{ "sample": rand.ExpFloat64(), } case c := <-b.queryrule: // deal with a query request c <- map[string]interface{}{ "rate": λ, } } } }
func TestTopK(t *testing.T) { stream := New(10) ss := []*Stream{New(10), New(10), New(10)} m := make(map[string]int) for _, s := range ss { for i := 0; i < 1e6; i++ { v := fmt.Sprintf("%x", int8(rand.ExpFloat64())) s.Insert(v) m[v]++ } stream.Merge(s.Query()) } var sm Samples for x, s := range m { sm = append(sm, &Element{x, s}) } sort.Sort(sort.Reverse(sm)) g := stream.Query() if len(g) != 10 { t.Fatalf("got %d, want 10", len(g)) } for i, e := range g { if sm[i].Value != e.Value { t.Errorf("at %d: want %q, got %q", i, sm[i].Value, e.Value) } } }
func TestRand() { fmt.Println("rand.Int()=", rand.Int()) fmt.Println("rand.Intn(10)=", rand.Intn(10)) fmt.Println("rand.Int31()=", rand.Int31()) fmt.Println("rand.Int31n(10)=", rand.Int31n(10)) fmt.Println("rand.Int63()=", rand.Int63()) fmt.Println("rand.Int63n(10)=", rand.Int63n(10)) fmt.Println("rand.Float32()=", rand.Float32()) fmt.Println("rand.Float64()=", rand.Float64()) fmt.Println("rand.Perm(10)=", rand.Perm(10)) fmt.Println("rand.ExpFloat64()=", rand.ExpFloat64()) fmt.Println("rand.Uint32()=", rand.Uint32()) r := rand.New(rand.NewSource(12346789)) fmt.Println("r.Int()=", r.Int()) fmt.Println("r.Intn(10)=", r.Intn(10)) fmt.Println("r.Int31()=", r.Int31()) fmt.Println("r.Int31n(10)=", r.Int31n(10)) fmt.Println("r.Int63()=", r.Int63()) fmt.Println("r.Int63n(10)=", r.Int63n(10)) fmt.Println("r.Float32()=", r.Float32()) fmt.Println("r.Float64()=", r.Float64()) fmt.Println("r.Perm(10)=", r.Perm(10)) fmt.Println("r.ExpFloat64()=", r.ExpFloat64()) fmt.Println("r.Uint32()=", r.Uint32()) }
// Example_boxPlots draws vertical boxplots. func Example_boxPlots() *plot.Plot { rand.Seed(int64(0)) n := 100 uniform := make(plotter.Values, n) normal := make(plotter.Values, n) expon := make(plotter.Values, n) for i := 0; i < n; i++ { uniform[i] = rand.Float64() normal[i] = rand.NormFloat64() expon[i] = rand.ExpFloat64() } p, err := plot.New() if err != nil { panic(err) } p.Title.Text = "Box Plot" p.Y.Label.Text = "plotter.Values" // Make boxes for our data and add them to the plot. p.Add(plotter.NewBoxPlot(vg.Points(20), 0, uniform), plotter.NewBoxPlot(vg.Points(20), 1, normal), plotter.NewBoxPlot(vg.Points(20), 2, expon)) // Set the X axis of the plot to nominal with // the given names for x=0, x=1 and x=2. p.NominalX("Uniform\nDistribution", "Normal\nDistribution", "Exponential\nDistribution") return p }
func RunUpdateHook(box string) { const MEAN = 5 const TIMEOUT = 5 * time.Second log.Println("Executing ", box) done := make(chan struct{}) cancelled := false go func() { time.Sleep(1*time.Second + time.Duration(1e9*rand.ExpFloat64()*MEAN)) close(done) if !cancelled { log.Println(" Completed ", box) } }() // cmd := exec.Command(work.command) // cmd.Start() // go func() { // cmd.Wait() // close(done) // }() select { case <-done: return case <-time.After(TIMEOUT): cancelled = true log.Println(" Cancelled ", box) // cmd.Process.Kill() } }
// startUser simulates a stream of user events until the stopper // indicates it's time to exit. func startUser(ctx Context, stopper *stop.Stopper) { for { userID := 1 + int(rand.ExpFloat64()/rate) op := randomOp() if err := stopper.RunTask(func() { err := runUserOp(ctx, userID, op.typ) stats.Lock() _ = stats.hist.RecordValue(int64(userID)) stats.totalOps++ stats.opCounts[op.typ]++ switch { case err == errNoUser: stats.noUserOps++ case err == errNoPhoto: stats.noPhotoOps++ case err != nil: stats.failedOps++ log.Printf("failed to run %s op for %d: %s", op.name, userID, err) } stats.Unlock() }); err != nil { return } } }
func (link *Link) Delay() time.Duration { link.Lock() defer link.Unlock() jitter := int64(rand.ExpFloat64() * float64(link.jitter)) latency := jitter + int64(link.spike) + int64(link.latency) return time.Duration(latency) * time.Millisecond }
func Example_groupedHorizontalQuartPlots() *plot.Plot { rand.Seed(int64(0)) n := 100 uniform := make(plotter.Values, n) normal := make(plotter.Values, n) expon := make(plotter.Values, n) for i := 0; i < n; i++ { uniform[i] = rand.Float64() normal[i] = rand.NormFloat64() expon[i] = rand.ExpFloat64() } p, err := plot.New() if err != nil { panic(err) } p.Title.Text = "Box Plot" p.Y.Label.Text = "plotter.Values" w := vg.Points(10) for x := 0.0; x < 3.0; x++ { b0 := must(plotter.MakeHorizQuartPlot(x, uniform)).(plotter.HorizQuartPlot) b0.Offset = -w b1 := must(plotter.MakeHorizQuartPlot(x, normal)).(plotter.HorizQuartPlot) b2 := must(plotter.MakeHorizQuartPlot(x, expon)).(plotter.HorizQuartPlot) b2.Offset = w p.Add(b0, b1, b2) } p.Add(plotter.NewGlyphBoxes()) p.NominalY("Group 0", "Group 1", "Group 2") return p }
// Example_quartPlots draws vertical quartile plots. func Example_quartPlots() *plot.Plot { rand.Seed(int64(0)) n := 100 uniform := make(plotter.Values, n) normal := make(plotter.Values, n) expon := make(plotter.Values, n) for i := 0; i < n; i++ { uniform[i] = rand.Float64() normal[i] = rand.NormFloat64() expon[i] = rand.ExpFloat64() } p, err := plot.New() if err != nil { panic(err) } p.Title.Text = "Quartile Plot" p.Y.Label.Text = "plotter.Values" p.Add(must(plotter.NewQuartPlot(0, uniform)).(*plotter.QuartPlot), must(plotter.NewQuartPlot(1, normal)).(*plotter.QuartPlot), must(plotter.NewQuartPlot(2, expon)).(*plotter.QuartPlot)) // Set the X axis of the plot to nominal with // the given names for x=0, x=1 and x=2. p.NominalX("Uniform\nDistribution", "Normal\nDistribution", "Exponential\nDistribution") return p }
// Example_groupedBoxPlots draws vertical boxplots. func Example_groupedBoxPlots() *plot.Plot { rand.Seed(int64(0)) n := 100 uniform := make(plotter.Values, n) normal := make(plotter.Values, n) expon := make(plotter.Values, n) for i := 0; i < n; i++ { uniform[i] = rand.Float64() normal[i] = rand.NormFloat64() expon[i] = rand.ExpFloat64() } p, err := plot.New() if err != nil { panic(err) } p.Title.Text = "Box Plot" p.Y.Label.Text = "plotter.Values" w := vg.Points(20) for x := 0.0; x < 3.0; x++ { b0 := must(plotter.NewBoxPlot(w, x, uniform)).(*plotter.BoxPlot) b0.Offset = -w - vg.Points(3) b1 := must(plotter.NewBoxPlot(w, x, normal)).(*plotter.BoxPlot) b2 := must(plotter.NewBoxPlot(w, x, expon)).(*plotter.BoxPlot) b2.Offset = w + vg.Points(3) p.Add(b0, b1, b2) } // Set the X axis of the plot to nominal with // the given names for x=0, x=1 and x=2. p.NominalX("Group 0", "Group 1", "Group 2") return p }
func TestSparseMatrix32(t *testing.T) { svd := NewGoRedSVD() assert.NotNil(t, svd) sparseMatrix32 := make(map[int]map[int]float32) for row := 0; row < 100; row++ { sparseMatrix32[row] = make(map[int]float32) for col := 0; col < 100; col++ { if rand.Float64() < 0.2 { sparseMatrix32[row][col] = float32(rand.ExpFloat64()) } } } assert.Equal(t, 100, len(sparseMatrix32)) svd.SetMatrix32(100, 100, sparseMatrix32) svd.RedSVD(10) u := svd.MatrixU() assert.NotNil(t, u) assert.Equal(t, 100, len(u)) assert.Equal(t, 10, len(u[0])) v := svd.MatrixV() assert.NotNil(t, v) assert.Equal(t, 100, len(v)) assert.Equal(t, 10, len(v[0])) singularValues := svd.SingularValues() assert.NotNil(t, singularValues) assert.Equal(t, 10, len(singularValues)) svd.SetUnnormalized(true) svd.RedSVD(10) u = svd.MatrixUNotNormalized() assert.NotNil(t, u) assert.Equal(t, 100, len(u)) assert.Equal(t, 10, len(u[0])) v = svd.MatrixVNotNormalized() assert.NotNil(t, v) assert.Equal(t, 100, len(v)) assert.Equal(t, 10, len(v[0])) singularValues = svd.SingularValues() assert.NotNil(t, singularValues) assert.Equal(t, 10, len(singularValues)) norms := svd.GetColumnNorms() assert.NotNil(t, norms) assert.Equal(t, 10, len(norms)) DeleteGoRedSVD(svd) }
func BenchmarkInsert10Bins(b *testing.B) { b.StopTimer() h := New(10) b.StartTimer() for i := 0; i < b.N; i++ { f := rand.ExpFloat64() h.Insert(f) } }
func addNoiseToSource(source func() ([]float64, int), strength float64) func() ([]float64, int) { return func() ([]float64, int) { data, period := source() for i := range data { data[i] += rand.ExpFloat64() * strength } return data, period } }
// Rand returns a random sample drawn from the distribution. func (e Exponential) Rand() float64 { var rnd float64 if e.Source == nil { rnd = rand.ExpFloat64() } else { rnd = e.Source.ExpFloat64() } return rnd / e.Rate }