溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

golang語言漸入佳境[30]-rand-package

發(fā)布時間:2020-07-25 00:44:35 來源:網絡 閱讀:206 作者:jonson_jackson 欄目:開發(fā)技術
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main

import (
"fmt"
"math/rand"
"time"
)

func main() {
randTest()

randAnswer()
}

func randTest() {
//1、通過默認的隨機數(shù)種子獲取隨機數(shù).
//系統(tǒng)默認的rand對象,隨機種子默認都是1
fmt.Println(rand.Int())
fmt.Println(rand.Intn(50))
fmt.Println(rand.Float64())

// 2、動態(tài)隨機種子,生成隨機資源,實例化成隨機對象,通過隨機對象獲取隨機數(shù)
s1 := rand.NewSource(time.Now().UnixNano())
r1 := rand.New(s1)
randnum := r1.Intn(10)
fmt.Println(randnum)

//3、簡寫形式:動態(tài)種子來獲取隨機數(shù)
// [0,10]
rand.Seed(time.Now().UnixNano())
fmt.Println(rand.Intn(10))

fmt.Println(rand.Float64())

//[5,11]
num := rand.Intn(7) + 5
fmt.Println(num)
}

func randAnswer() {
answers := []string{
"It is certain",
"It is decidedly so",
"Without a doubt",
"Yes definitely",
"You may rely on it",
"As I see it yes",
"Most likely",
"Outlook good",
"Yes",
"Signs point to yes",
"Reply hazy try again",
"Ask again later",
"Better not tell you now",
"Cannot predict now",
"Concentrate and ask again",
"Don't count on it",
"My reply is no",
"My sources say no",
"Outlook not so good",
"Very doubtful",
}
rand.Seed(time.Now().UnixNano())
randnum := rand.Intn(len(answers))
fmt.Println("隨機回答", answers[randnum])
}
  • 本文鏈接: https://dreamerjonson.com/2018/12/01/golang-30-rand-package/

  • 版權聲明: 本博客所有文章除特別聲明外,均采用 CC BY 4.0 CN協(xié)議 許可協(xié)議。轉載請注明出處!

golang語言漸入佳境[30]-rand-package

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI