本文整理汇总了Golang中math/rand.Seed函数的典型用法代码示例。如果您正苦于以下问题:Golang Seed函数的具体用法?Golang Seed怎么用?Golang Seed使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Seed函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: init
func init() {
rand.Seed(time.Now().UnixNano())
http.HandleFunc("/dirtexto", directorioTexto)
//http.HandleFunc("/carr", carr)
http.HandleFunc("/carr", carrVip)
rand.Seed(time.Now().UnixNano())
}
示例2: Start
// Start the agent-based model
func (m *Model) Start() error {
if m.running {
return errors.New("Model: Start() failed: model already running")
}
m.running = true
if m.RNGRandomSeed {
rand.Seed(time.Now().UnixNano())
} else {
rand.Seed(m.RNGSeedVal)
}
timestamp := fmt.Sprintf("%s", time.Now())
m.popCpPrey = GenerateCpPreyPopulation(m.CpPreyPopulationStart, m.numCpPreyCreated, m.Turn, m.ConditionParams, timestamp)
m.numCpPreyCreated += m.CpPreyPopulationStart
m.popVisualPredator = GenerateVPredatorPopulation(m.VpPopulationStart, m.numVpCreated, m.Turn, m.ConditionParams, timestamp)
m.numVpCreated += m.VpPopulationStart
if m.Logging {
go m.log(m.e)
}
if m.Visualise {
go m.vis(m.e)
}
time.Sleep(pause)
go m.run(m.e)
return nil
}
示例3: main
func main() {
fmt.Println("My favortie number is ", rand.Intn(10))
fmt.Println("Another random int", rand.Intn(10))
// Try to Seed with 1
rand.Seed(1)
fmt.Println("First sequence")
for i := 0; i < 10; i++ {
fmt.Println(rand.Intn(10))
}
// Seed again with 1: Numbers are the same
rand.Seed(1)
fmt.Println("Second sequence")
for i := 0; i < 10; i++ {
fmt.Println(rand.Intn(10))
}
// Seed with 2
rand.Seed(2)
fmt.Println("Third sequence")
for i := 0; i < 10; i++ {
fmt.Println(rand.Intn(10))
}
}
示例4: main
func main() {
var i = 0
var buf []byte
// Repeatable payloads.
if seed, err := strconv.ParseInt(os.Getenv("SEED"), 10, 32); err == nil {
rand.Seed(seed)
} else {
rand.Seed(defaultSeed)
}
sleepTime, err := parseRate(os.Getenv("RATE"))
if err != nil {
log.Println("Duration Parsing: ", err)
log.Println("Continuing w/o duration")
}
mlen, err := strconv.ParseInt(os.Getenv("MSGSIZE"), 10, 32)
if err == nil && mlen > 0 {
buf = make([]byte, mlen)
} else {
log.Println("Unable to parse MSGSIZE, defaulting to MSGSIZE of", defaultMsgSize)
buf = make([]byte, defaultMsgSize)
}
for {
i++
if sleepTime > 0 {
time.Sleep(sleepTime)
}
fmt.Println(i, "spews", randStr(buf))
}
}
示例5: seedPRNG
// seed PRNG to value given, if it is 0 seed to current time
func seedPRNG(seed int64) {
if seed == 0 {
rand.Seed(time.Now().UTC().UnixNano())
} else {
rand.Seed(seed)
}
}
示例6: main
//pada fungsi ini akan menyimpan data yang digunakan sebagai input
func main() {
// change "64" to anything else to see
rand.Seed(8)
sl := New()
sl.Set(Int(6), 1)
sl.Set(Int(5), 2)
sl.Set(Int(4), 4)
sl.Set(Int(9), 0)
sl.draw() // 4-5-6-9-
valuenya, ketemu := sl.Search(Int(5)) // Found! = 5
fmt.Println(valuenya) //2
fmt.Println(ketemu) // true
valuenya, ketemu = sl.Search(Int(4)) // Found! = 4
fmt.Println(valuenya) //4
fmt.Println(ketemu) // true
valuenya, ketemu = sl.Search(Int(6)) // Found! = 6
fmt.Println(valuenya) //1
fmt.Println(ketemu) // true
valuenya, ketemu = sl.Search(Int(9)) // Found! = 9
fmt.Println(valuenya) //0
fmt.Println(ketemu) // true
sl.Delete(Int(5))
sl.draw() // 4---6-9-
valuenya, ketemu = sl.Search(Int(5)) // No Match Found!
fmt.Println(valuenya) //<nil>
fmt.Println(ketemu) // false
sl.draw() // 4---6-9-
rand.Seed(8)
s2 := New()
s2.Set(String("h"), 1)
s2.Set(String("c"), 2)
s2.Set(String("i"), 4)
s2.Set(String("s"), 0)
s2.draw() // c-h-i-s-
valuenya, ketemu = s2.Search(String("c")) // Found! = c
fmt.Println(valuenya) //2
fmt.Println(ketemu) // true
valuenya, ketemu = s2.Search(String("i")) // Found! = i
fmt.Println(valuenya) //4
fmt.Println(ketemu) // true
valuenya, ketemu = s2.Search(String("s")) // Found! = s
fmt.Println(valuenya) //0
fmt.Println(ketemu) // true
valuenya, ketemu = s2.Search(String("h")) // Found! = h
fmt.Println(valuenya) //1
fmt.Println(ketemu) // true
s2.Delete(String("i"))
s2.draw() // c-h---s-
valuenya, ketemu = s2.Search(String("i")) // No Match Found!
fmt.Println(valuenya) //<nil>
fmt.Println(ketemu) // false
s2.draw() // c-h---s-
}
示例7: main
func main() {
IntSlice := make(IntArr, 12)
fmt.Print("Awal : ")
for i := 0; i < 12; i++ {
rand.Seed(time.Now().UTC().UnixNano())
IntSlice[i] = rand.Intn(50)
fmt.Print(IntSlice[i], " ")
}
fmt.Println()
CocktailSort(IntSlice)
fmt.Print("Sesudah Sorting : ")
for i := 0; i < 12; i++ {
fmt.Print(IntSlice[i], " ")
}
fmt.Println("\n")
StringSlice := StringArr{"nama", "saya", "adalah", "alvreda", "ivena"}
fmt.Print("Awal : ")
for i := 0; i < 5; i++ {
fmt.Print(StringSlice[i], " ")
}
fmt.Println()
CocktailSort(StringSlice)
fmt.Print("Sesudah Sorting : ")
for i := 0; i < 5; i++ {
fmt.Print(StringSlice[i], " ")
}
fmt.Println("\n")
Float32Slice := make(Float32Arr, 6)
fmt.Print("Awal : ")
for i := 0; i < 6; i++ {
rand.Seed(time.Now().UTC().UnixNano())
Float32Slice[i] = rand.Float32()
fmt.Print(Float32Slice[i], " ")
}
fmt.Println()
CocktailSort(Float32Slice)
fmt.Print("Sesudah Sorting : ")
for i := 0; i < 6; i++ {
fmt.Print(Float32Slice[i], " ")
}
fmt.Println("\n")
Float64Slice := make(Float64Arr, 6)
fmt.Print("Awal : ")
for i := 0; i < 6; i++ {
rand.Seed(time.Now().UTC().UnixNano())
Float64Slice[i] = rand.Float64()
fmt.Print(Float64Slice[i], " ")
}
fmt.Println()
CocktailSort(Float64Slice)
fmt.Print("Sesudah Sorting : ")
for i := 0; i < 6; i++ {
fmt.Print(Float64Slice[i], " ")
}
fmt.Println()
}
示例8: TestReserveOneVolume
func TestReserveOneVolume(t *testing.T) {
topo := setup(topologyLayout)
rand.Seed(time.Now().UnixNano())
rand.Seed(1)
ret, node, vid := topo.RandomlyReserveOneVolume()
fmt.Println("assigned :", ret, ", node :", node, ", volume id:", vid)
}
示例9: randomDate
func randomDate() time.Time {
rand.Seed(time.Now().Unix())
randomMonth := rand.Intn(108)
rand.Seed(time.Now().Unix() + int64(randomMonth))
randomDay := rand.Intn(30)
return time.Now().AddDate(0, -randomMonth, -randomDay)
}
示例10: TestRetry
func TestRetry(t *testing.T) {
defer func() {
timeAfter = time.After
}()
rh := &Helper{
Backoff: 2,
Retries: 5,
Delay: 300 * time.Millisecond,
MaxDelay: 2 * time.Second,
MaxJitter: 5 * time.Millisecond,
}
durations := []time.Duration{
300 * time.Millisecond,
600 * time.Millisecond,
1200 * time.Millisecond,
2 * time.Second,
2 * time.Second,
}
rand.Seed(1)
for i := range durations {
jitter := rand.Int63n(int64(rh.MaxJitter))
durations[i] += time.Duration(jitter)
}
actualRetries := 0
timeAfter = func(d time.Duration) <-chan time.Time {
if actualRetries >= len(durations) {
t.Fatalf("Maximum retry attempts exceeded: got %d", actualRetries)
}
if d != durations[actualRetries] {
t.Errorf("Mismatched duration: got %s; want %s", d, durations[actualRetries])
}
actualRetries++
c := make(chan time.Time, 1)
c <- time.Now().Add(d)
return c
}
noErrFunc := func() error { return nil }
rand.Seed(1)
retries, err := rh.RetryFunc(noErrFunc)
if err != nil {
t.Errorf("RetryFunc returned error for non-error function: %s", err)
}
if retries != 0 {
t.Errorf("Mismatched retry attempt count: got %d", retries)
}
lastErr := &retryErr{5, true}
errFunc := func() (err error) {
return lastErr
}
rand.Seed(1)
retries, err = rh.RetryFunc(errFunc)
if retries != actualRetries {
t.Errorf("Mismatched retry attempt count: got %d; want %d", retries, actualRetries)
}
if err != lastErr {
t.Errorf("Unexpected retry error: got %q; want %q", err, lastErr)
}
}
示例11: init
// Seed sequential random with crypto or math/random and current time
// and generate crypto prefix.
func init() {
r, err := rand.Int(rand.Reader, big.NewInt(math.MaxInt64))
if err != nil {
prand.Seed(time.Now().UnixNano())
} else {
prand.Seed(r.Int64())
}
globalNUID = &lockedNUID{NUID: New()}
globalNUID.RandomizePrefix()
}
示例12: seedRand
func seedRand() {
n, err := rand.Int(rand.Reader, big.NewInt(9223372036854775806))
if err != nil {
mathrand.Seed(time.Now().UTC().UnixNano())
return
}
mathrand.Seed(n.Int64())
return
}
示例13: initGo
func initGo() {
fmt.Printf("Initializing Go System (%d threads)\n", numProcs)
if debug {
rt.GOMAXPROCS(2)
rand.Seed(0)
} else {
rt.GOMAXPROCS(numProcs)
rand.Seed(rand.Int63())
}
}
示例14: init
func init() {
// Seed the math/rand RNG from crypto/rand.
s := make([]byte, 8)
if _, err := crand.Read(s); err == nil {
rand.Seed(int64(binary.BigEndian.Uint64(s)))
return
}
// Fall back to system time, boo hoo.
rand.Seed(time.Now().UnixNano())
}
示例15: testSingleProposerUpdateKey
/**
* Tests that the key is correctly updated in each node's storage system.
*/
func testSingleProposerUpdateKey(doneChan chan bool) {
key := "b"
// send first proposal
randint, _ := crand.Int(crand.Reader, big.NewInt(math.MaxInt64))
rand.Seed(randint.Int64())
value := rand.Uint32()
if _, ok := pt.cliMap[1]; !ok {
LOGE.Println("FAIL: missing node 1")
doneChan <- false
return
}
pnum, err := pt.GetNextProposalNumber(key, 1)
reply, err := pt.Propose(key, value, pnum.N, 1)
if err != nil {
printFailErr("Propose", err)
doneChan <- false
return
}
// send second proposal
randint, _ = crand.Int(crand.Reader, big.NewInt(math.MaxInt64))
rand.Seed(randint.Int64())
value = rand.Uint32()
pnum2, err := pt.GetNextProposalNumber(key, 1)
// the proposal numbers should be monotonically increasing
if pnum.N >= pnum2.N {
LOGE.Println("FAIL: newer proposal number is less than or equal to older proposal number")
doneChan <- false
return
}
reply, err = pt.Propose(key, value, pnum2.N, 1)
if err != nil {
printFailErr("Propose", err)
doneChan <- false
return
}
if !checkProposeReply(reply, key, value) {
doneChan <- false
return
}
if !checkGetValueAll(key, value) {
doneChan <- false
return
}
doneChan <- true
}