本文整理汇总了Golang中math/rand.Int63n函数的典型用法代码示例。如果您正苦于以下问题:Golang Int63n函数的具体用法?Golang Int63n怎么用?Golang Int63n使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Int63n函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: FindParent
func (p *Plan) FindParent(materials material.MaterialSlice) string {
seed := time.Now().UnixNano()
fmt.Println("Seed:", seed)
rand.Seed(seed) // FIXME use own PRNG instance
ingredients := make([]Ingredient, 0)
for use, amount := range p.Requirements {
possibleMaterials := materials.FindByUse(use)
remainingAmount := amount
for remainingAmount > 0 {
randomAmount := rand.Int63n(int64(remainingAmount)) + 1
randomIndex := rand.Int63n(int64(len(possibleMaterials)))
randomGrade := rand.Int63n(5)
mat := possibleMaterials[randomIndex]
ingredient := Ingredient{mat, use, material.GradeIndexToGrade(randomGrade), uint8(randomAmount)}
ingredients = append(ingredients, ingredient)
remainingAmount -= uint8(randomAmount)
}
}
recipe := &Recipe{p, ingredients}
genes := recipe.genome(materials)
fmt.Println("Genome:", genes)
return genes
}
示例2: Fetch
// Requirement is TaskGroup
// Object is Agent's Location
func (s *Scheduler) Fetch(demands []market.Demand) {
var request resource.AllocationRequest
for _, d := range demands {
demand := d.Requirement.(*plan.TaskGroup)
request.Requests = append(request.Requests, resource.ComputeRequest{
ComputeResource: resource.ComputeResource{
CPUCount: 1,
CPULevel: 1,
MemoryMB: int64(s.Option.TaskMemoryMB),
},
Inputs: s.findTaskGroupInputs(demand),
})
}
result, err := Assign(s.Leader, &request)
if err != nil {
log.Printf("%s Failed to allocate: %v", s.Leader, err)
time.Sleep(time.Millisecond * time.Duration(15000+rand.Int63n(5000)))
} else {
if len(result.Allocations) == 0 {
log.Printf("%s Failed to allocate any executor.", s.Leader)
time.Sleep(time.Millisecond * time.Duration(2000+rand.Int63n(1000)))
} else {
log.Printf("%s allocated %d executors.", s.Leader, len(result.Allocations))
for _, allocation := range result.Allocations {
s.Market.AddSupply(market.Supply{
Object: allocation,
})
}
}
}
}
示例3: TestDotInt64WithDiffLength
func TestDotInt64WithDiffLength(t *testing.T) {
N := 1000 + rand.Intn(1000000)
M := 1000 + rand.Intn(1000000)
a := make([]int64, N)
b := make([]int64, M)
Expected := int64(0)
for i := range a {
if i < N {
a[i] = gomath.ScaleInt64(LowInt64, HighInt64, 0, HighInt64, rand.Int63n(HighInt64))
}
if i < M {
b[i] = gomath.ScaleInt64(LowInt64, HighInt64, 0, HighInt64, rand.Int63n(HighInt64))
}
if i < N && i < M {
Expected += a[i] * b[i]
}
}
Computed := DotInt64(a, b)
if Computed != Expected {
t.Logf("Expected %d but computed %d\n", Expected, Computed)
t.FailNow()
}
}
示例4: GetRandomValues
// GetRandomValues RPC call for OST, returns random values for testing
func (*OssRpcT) GetRandomValues(init bool, result *OstValues) error {
result.OstTotal = make(map[string]OstStats)
result.NidValues = make(map[string]map[string]OstStats)
for i := 0; i < 10; i++ {
ost := "OST" + strconv.Itoa(int(rand.Int63n(10)))
t := result.OstTotal[ost]
t.RBs = rand.Int63n(10)
t.WBs = rand.Int63n(10)
t.RRqs = rand.Int63n(100)
t.WRqs = rand.Int63n(100)
result.OstTotal[ost] = t
result.NidValues[ost] = make(map[string]OstStats)
for j := 0; j < 100; j++ {
nid := "nid" + strconv.Itoa(int(rand.Int63n(100)))
t := result.NidValues[ost][nid]
t.RBs = rand.Int63n(10)
t.WBs = rand.Int63n(10)
t.RRqs = rand.Int63n(100)
t.WRqs = rand.Int63n(100)
result.NidValues[ost][nid] = t
}
}
return nil
}
示例5: hyperWatch
func (self *sector) hyperWatch() {
ratio := int64(1e3)
for {
// Keeping number of players in field constant.
if len(self.players) < 20 {
ai := newAgent()
ai.player.Position = [2]float64{
float64(rand.Int63n(ratio) - ratio/2),
float64(rand.Int63n(ratio) - ratio/2),
}
self.addPlayer(ai.player)
}
// Keeping number of items constant.
if len(self.powerups) < 30 {
pu := newPowerup()
// Random position.
pu.Position = [2]float64{
float64(rand.Int63n(self.bounds[0]) - self.bounds[0]/2),
float64(rand.Int63n(self.bounds[1]) - self.bounds[1]/2),
}
self.addPowerup(pu)
}
log.Printf("players: %v, items: %v\n", len(self.players), len(self.powerups))
time.Sleep(time.Millisecond * time.Duration(rand.Float32()*hyperWatchMaxTime))
}
}
示例6: loadData
func loadData() {
var DOCS_SIZE uint32 = 50000
var TAGS_SIZE uint64 = 1024
docs = NewDocuments(DOCS_SIZE, TAGS_SIZE)
var i uint32
for i = 0; i < DOCS_SIZE; i++ {
d := bitset.New64(TAGS_SIZE)
d.Set(1)
for j := 0; j < 200; j++ {
d.Set(uint64(rand.Int63n(int64(TAGS_SIZE))))
}
docs.Set(i, d)
}
d := bitset.New64(TAGS_SIZE)
d.Set(1)
for j := 0; j < 200; j++ {
d.Set(uint64(rand.Int63n(int64(TAGS_SIZE))))
}
for i = 1; i < 5; i++ {
d := bitset.New64(TAGS_SIZE)
for j := 0; j < 200; j++ {
d.Set(uint64(rand.Int63n(int64(TAGS_SIZE))))
}
docs.bonus[i] = d
}
}
示例7: TestRead
func TestRead(t *testing.T) {
compare := func(offset int64, size int64) {
l := size - offset
buf := make([]byte, l)
res, code := objfile.Read(buf, offset)
if !code.Ok() {
t.Fatalf("Read() error, (not OK)")
}
read, _ := res.Bytes(buf)
data := string(read)
if TEST_DATA[offset:offset+l] != data {
t.Fatalf("Read() returns invalid data. (offset:%d, size:%d)", offset, size)
}
}
var i = 0
for i < 10 {
size := rand.Int63n(int64(len(TEST_DATA)))
offset := rand.Int63n(size - 1)
compare(offset, size)
i++
}
}
示例8: init
func init() {
nodes = make([]element.Node, 64)
offset := rand.Int63n(1e10)
for i := range nodes {
nodes[i] = element.Node{OSMElem: element.OSMElem{Id: offset + rand.Int63n(1000)}, Long: rand.Float64()*360 - 180, Lat: rand.Float64()*180 - 90}
}
}
示例9: GenerateRandomStats
func GenerateRandomStats(numStats, numCores int, duration time.Duration) []*info.ContainerStats {
ret := make([]*info.ContainerStats, numStats)
perCoreUsages := make([]uint64, numCores)
currentTime := time.Now()
for i := range perCoreUsages {
perCoreUsages[i] = uint64(rand.Int63n(1000))
}
for i := 0; i < numStats; i++ {
stats := new(info.ContainerStats)
stats.Timestamp = currentTime
currentTime = currentTime.Add(duration)
percore := make([]uint64, numCores)
for i := range perCoreUsages {
perCoreUsages[i] += uint64(rand.Int63n(1000))
percore[i] = perCoreUsages[i]
stats.Cpu.Usage.Total += percore[i]
}
stats.Cpu.Usage.PerCpu = percore
stats.Cpu.Usage.User = stats.Cpu.Usage.Total
stats.Cpu.Usage.System = 0
stats.Memory.Usage = uint64(rand.Int63n(4096))
ret[i] = stats
}
return ret
}
示例10: BeginTransaction
/*
Begins an immediate-mode database transaction.
*/
func (db *SqliteDBThread) BeginTransaction(transactionType string) (err error) {
db.dbt.th.AllowGC()
defer db.dbt.th.DisallowGC()
stmt := "BEGIN " + transactionType + " TRANSACTION"
err = db.ExecStatement(stmt)
var TRY_GAP_WIDENING_MS_INCREMENT int64 = 100 // ms - longest wait will be 6 seconds + random factor
var tryGapWidth int64 = TRY_GAP_WIDENING_MS_INCREMENT
var r int64
for i := 0; err != nil && i < N_BEGIN_TRIES; i++ {
Logln(PERSIST2_, "BEGIN", transactionType, "ERR:", err)
r = rand.Int63n(500 + tryGapWidth)
if i > 5 {
TRY_GAP_WIDENING_MS_INCREMENT = 1000
}
tryGapWidth += TRY_GAP_WIDENING_MS_INCREMENT
time.Sleep(time.Duration((tryGapWidth + r) * 1000000))
err = db.ExecStatement(stmt)
}
if transactionType == "DEFERRED" {
// We really don't want a deferred transaction. We want to create a SHARED lock right
// away in the SQLITE database. If we cannot, we need to fail-fast here, so that
// the code inside the transaction-protected block will not be executed.
// So do a dummy read query.
if dummyQuery == nil {
dummyQuery, err = db.Prepare("select rowid from RPackage where rowid=1")
if err != nil {
panic(err)
}
}
err = dummyQuery.Query()
dummyQuery.Reset()
var TRY_GAP_WIDENING_MS_INCREMENT int64 = 100 // ms - longest wait will be 6 seconds + random factor
var tryGapWidth int64 = TRY_GAP_WIDENING_MS_INCREMENT
var r int64
for j := 0; err != nil && j < N_BEGIN_TRIES; j++ {
Logln(PERSIST2_, "BEGIN DEFERRED: ERR executing dummy select query:", err)
r = rand.Int63n(500 + tryGapWidth)
if j > 5 {
TRY_GAP_WIDENING_MS_INCREMENT = 1000
}
tryGapWidth += TRY_GAP_WIDENING_MS_INCREMENT
time.Sleep(time.Duration((tryGapWidth + r) * 1000000))
err = dummyQuery.Query()
dummyQuery.Reset()
}
}
if err == nil {
Logln(PERSIST2_, ">>>>>>>>>>>>>>>>>>>>>>>>> SUCCESSFULLY BEGAN", transactionType, "TRANSACTION")
}
return
}
示例11: getSignedTrans
func getSignedTrans() IBlock {
if nb != nil {
return nb
}
nb = new(Transaction)
t := nb.(*Transaction)
for i := 0; i < 5; i++ {
t.AddInput(nextAddress(), uint64(rand.Int63n(10000000000)))
}
for i := 0; i < 3; i++ {
t.AddOutput(nextAddress(), uint64(rand.Int63n(10000000000)))
}
for i := 0; i < 3; i++ {
t.AddECOutput(nextAddress(), uint64(rand.Int63n(10000000)))
}
for i := 0; i < 3; i++ {
sig := NewRCD_1(nextSig())
t.AddAuthorization(sig)
}
for i := 0; i < 2; i++ {
t.AddAuthorization(nextAuth2())
}
return nb
}
示例12: newFactoidTransaction
func newFactoidTransaction() *FactoidTransaction {
msg := new(FactoidTransaction)
t := new(factoid.Transaction)
for i := 0; i < 5; i++ {
t.AddInput(nextAddress(), uint64(rand.Int63n(10000000000)))
}
for i := 0; i < 3; i++ {
t.AddOutput(nextAddress(), uint64(rand.Int63n(10000000000)))
}
for i := 0; i < 3; i++ {
t.AddECOutput(nextAddress(), uint64(rand.Int63n(10000000)))
}
for i := 0; i < 3; i++ {
sig := factoid.NewRCD_1(nextSig())
t.AddAuthorization(sig)
}
for i := 0; i < 2; i++ {
t.AddAuthorization(nextAuth2())
}
msg.Transaction = t
return msg
}
示例13: Action
func (p *Producer) Action() {
var w Work
println("Producer Action")
w.start = current + time(rand.Int63n(100))
clock.insert(&w)
p.start = current + time(rand.Int63n(100))
clock.insert(p)
}
示例14: TestMonitorServer
func TestMonitorServer(t *testing.T) {
config := &hydra.Config{
Monitor: &hydra.ConfigMonitor{
Host: "localhost",
Port: 0,
},
}
_, ch := hydra.NewChannel()
monitor, _ := hydra.NewMonitor(config, ch)
go monitor.Run()
expectedMessages := make(map[string]int64)
expectedBytes := make(map[string]int64)
tags := []string{"foo", "bar", "dummy.test"}
for _, tag := range tags {
for i := 1; i <= 100; i++ {
m := rand.Int63n(10)
b := rand.Int63n(2560)
ch <- &hydra.SentStat{
Tag: tag,
Messages: m,
Bytes: b,
}
expectedMessages[tag] += m
expectedBytes[tag] += b
}
}
sleep(1)
resp, err := http.Get(fmt.Sprintf("http://%s/", monitor.Addr))
if err != nil {
t.Error(err)
}
defer resp.Body.Close()
if ct := resp.Header.Get("Content-Type"); ct != "application/json" {
t.Error("invalid content-type", ct)
}
body, _ := ioutil.ReadAll(resp.Body)
js := bytes.NewReader(body)
for tag, n := range expectedMessages {
js.Seek(int64(0), os.SEEK_SET)
var got int64
scan.ScanJSON(js, "/sent/"+tag+"/messages", &got)
if got != n {
t.Errorf("/sent/%s/messages got %d expected %d", tag, got, n)
}
}
for tag, n := range expectedBytes {
js.Seek(int64(0), os.SEEK_SET)
var got int64
scan.ScanJSON(js, "/sent/"+tag+"/bytes", &got)
if got != n {
t.Errorf("/sent/%s/bytes got %d expected %d", tag, got, n)
}
}
log.Println(string(body))
}
示例15: requester
func requester(work chan Request) {
for {
time.Sleep(time.Duration(rand.Int63n(MaxRequesters * Seconds)))
work <- func() {
r := rand.Int63n(MaxRequesters*Seconds) + 10
time.Sleep(time.Duration(r))
}
}
}