概述
func main(){
guessNum(58)
}
/**
猜数字
*/
func guessNum(insertNum int) {
temp := 0
n := 0
for {
temp = randNum(int64(n))
n++
if (temp != insertNum) {
println("Not equal,Now is ",temp)
} else {
println("Equal! Now is",temp)
break
}
}
}
/**
任意数字
*/
func randNum(seedOther int64) int{
// 时间戳
var timeStamp = time.Now().Unix()
// go速度还是太快,同一时间内种子是同一个,加上一个自增数字区分
r := rand.New(rand.NewSource(timeStamp+seedOther))
num := r.Intn(100)
return num
}
运行结果
Not equal,Now is 1
Not equal,Now is 91
Not equal,Now is 76
Not equal,Now is 71
Not equal,Now is 36
Not equal,Now is 17
Not equal,Now is 75
Not equal,Now is 32
Not equal,Now is 46
Not equal,Now is 16
Not equal,Now is 66
Not equal,Now is 91
Not equal,Now is 25
Not equal,Now is 38
Not equal,Now is 55
Not equal,Now is 60
Not equal,Now is 86
Not equal,Now is 67
Not equal,Now is 52
Not equal,Now is 90
Not equal,Now is 51
Not equal,Now is 54
Not equal,Now is 43
Not equal,Now is 85
Not equal,Now is 22
Not equal,Now is 36
Not equal,Now is 89
Not equal,Now is 27
Not equal,Now is 57
Not equal,Now is 70
Not equal,Now is 36
Not equal,Now is 77
Not equal,Now is 23
Not equal,Now is 88
Equal! Now is 58
最后
以上就是坦率火龙果为你收集整理的golang猜数字 100以内随机数的全部内容,希望文章能够帮你解决golang猜数字 100以内随机数所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复