golang语言渐入佳境[30]-rand-package

发布时间:2020-07-25 00:44:35 作者:jonson_jackson
来源:网络 阅读:195
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、通过默认的随机数种子获取随机数.
//系统默认的rand对象,随机种子默认都是1
fmt.Println(rand.Int())
fmt.Println(rand.Intn(50))
fmt.Println(rand.Float64())

// 2、动态随机种子,生成随机资源,实例化成随机对象,通过随机对象获取随机数
s1 := rand.NewSource(time.Now().UnixNano())
r1 := rand.New(s1)
randnum := r1.Intn(10)
fmt.Println(randnum)

//3、简写形式:动态种子来获取随机数
// [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])
}

golang语言渐入佳境[30]-rand-package

推荐阅读:
  1. 以太坊如何计算交易成本
  2. 学习区块链有什么用处

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

区块链 go age

上一篇:Android平台的手机记账应用开发全程实录

下一篇:Mac终端下,一些常见快捷键

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》