当前位置: 首页 > news >正文

个人网站备案后做游戏宁波seo关键词优化报价

个人网站备案后做游戏,宁波seo关键词优化报价,vps挂网站,专业网站建设案例1. Go中的随机数生成 在许多编程任务中,随机数的生成是不可或缺的。Go语言通过 math/rand 包提供了伪随机数生成方式。伪随机数由种子(seed)决定,如果种子相同,生成的数列也会相同。为了确保每次程序运行时产生不同的随机数,我们…

1. Go中的随机数生成

在许多编程任务中,随机数的生成是不可或缺的。Go语言通过 math/rand 包提供了伪随机数生成方式。伪随机数由种子(seed)决定,如果种子相同,生成的数列也会相同。为了确保每次程序运行时产生不同的随机数,我们通常使用当前时间作为种子。

示例1:简单的随机数生成
package mainimport ("fmt""math/rand""time"
)func random(min, max int) int {return rand.Intn(max-min) + min
}func main() {rand.Seed(time.Now().Unix())for i := 0; i < 5; i++ {fmt.Println("生成的随机数:", random(0, 100))}
}

上述程序使用了当前时间作为种子来生成0到100之间的5个随机数。输出结果每次都会不同。

示例2:通过命令行控制随机数生成

为了使程序更具灵活性,我们可以通过命令行传入参数来控制随机数的生成范围和数量,甚至可以指定种子来生成固定序列的伪随机数。

package mainimport ("fmt""math/rand""os""strconv""time"
)func random(min, max int) int {return rand.Intn(max-min) + min
}func main() {MIN := 0MAX := 100TOTAL := 10SEED := time.Now().Unix()arguments := os.Argsswitch len(arguments) {case 2:MIN, _ = strconv.Atoi(arguments[1])MAX = MIN + 100case 3:MIN, _ = strconv.Atoi(arguments[1])MAX, _ = strconv.Atoi(arguments[2])case 4:MIN, _ = strconv.Atoi(arguments[1])MAX, _ = strconv.Atoi(arguments[2])TOTAL, _ = strconv.Atoi(arguments[3])case 5:MIN, _ = strconv.Atoi(arguments[1])MAX, _ = strconv.Atoi(arguments[2])TOTAL, _ = strconv.Atoi(arguments[3])SEED, _ = strconv.ParseInt(arguments[4], 10, 64)default:fmt.Println("使用默认值")}rand.Seed(SEED)for i := 0; i < TOTAL; i++ {fmt.Printf("%d ", random(MIN, MAX))}fmt.Println()
}

通过不同的命令行参数,可以控制生成的随机数。例如:

$ go run randomNumbers.go 10 50 5
14 37 27 49 16

这段代码生成了5个在10到50之间的随机数。

2. 加密级别的随机数生成

在安全领域,生成密码时,伪随机数不足以提供安全性。Go提供了 crypto/rand 包来生成加密安全的随机数,它可以生成不可预测的随机序列。

示例3:生成加密安全的随机密码
package mainimport ("crypto/rand""encoding/base64""fmt""os""strconv"
)func generateBytes(n int64) ([]byte, error) {b := make([]byte, n)_, err := rand.Read(b)if err != nil {return nil, err}return b, nil
}func generatePass(s int64) (string, error) {b, err := generateBytes(s)return base64.URLEncoding.EncodeToString(b), err
}func main() {var LENGTH int64 = 8arguments := os.Argsif len(arguments) == 2 {LENGTH, _ = strconv.ParseInt(arguments[1], 10, 64)}myPass, err := generatePass(LENGTH)if err != nil {fmt.Println("生成密码失败:", err)return}fmt.Println("生成的密码:", myPass[0:LENGTH])
}

运行程序后会生成一个随机密码,例如:

生成的密码: Zm9yQ29kZT==

3. 矩阵运算与生成

矩阵在图像处理、机器学习等领域中有广泛应用。我们可以利用Go语言生成随机矩阵,并进行矩阵加法、减法等运算。

示例4:生成随机矩阵并进行矩阵加法
package mainimport ("fmt""math/rand""time"
)func generateMatrix(row, col int) [][]int {matrix := make([][]int, row)for i := range matrix {matrix[i] = make([]int, col)for j := range matrix[i] {matrix[i][j] = rand.Intn(10)}}return matrix
}func addMatrices(m1, m2 [][]int) [][]int {result := make([][]int, len(m1))for i := range m1 {result[i] = make([]int, len(m1[i]))for j := range m1[i] {result[i][j] = m1[i][j] + m2[i][j]}}return result
}func main() {rand.Seed(time.Now().Unix())m1 := generateMatrix(3, 3)m2 := generateMatrix(3, 3)fmt.Println("矩阵1:", m1)fmt.Println("矩阵2:", m2)result := addMatrices(m1, m2)fmt.Println("矩阵加法结果:", result)
}

通过上述代码,您可以生成两个3x3的随机矩阵,并执行矩阵加法运算。

4. 数独验证程序

数独是一个流行的逻辑益智游戏,要求将1到9填入一个9x9的网格中,使每行、每列和每个3x3的子区域都包含不重复的数字。我们可以通过编写程序验证数独是否解得正确。

示例5:数独验证
package mainimport ("bufio""errors""fmt""os""strconv""strings"
)func importFile(file string) ([][]int, error) {var err errorvar mySlice = make([][]int, 0)f, err := os.Open(file)if err != nil {return nil, err}defer f.Close()r := bufio.NewReader(f)for {line, err := r.ReadString('\n')fields := strings.Fields(line)temp := make([]int, 0)for _, v := range fields {n, err := strconv.Atoi(v)if err != nil {return nil, err}temp = append(temp, n)}if len(temp) != 0 {mySlice = append(mySlice, temp)}if err != nil {break}}return mySlice, nil
}func validPuzzle(sl [][]int) bool {for i := 0; i <= 2; i++ {for j := 0; j <= 2; j++ {iEl := i * 3jEl := j * 3mySlice := []int{0, 0, 0, 0, 0, 0, 0, 0, 0}for k := 0; k <= 2; k++ {for m := 0; m <= 2; m++ {bigI := iEl + kbigJ := jEl + mval := sl[bigI][bigJ]if val > 0 && val < 10 {if mySlice[val-1] == 1 {fmt.Println("数字出现两次:", val)return false} else {mySlice[val-1] = 1}} else {fmt.Println("无效值:", val)return false}}}}}for i := 0; i <= 8; i++ {sum := 0for j := 0; j <= 8; j++ {sum = sum + sl[i][j]}if sum != 45 {return false}sum = 0}for i :=0; i <= 8; i++ {sum := 0for j := 0; j <= 8; j++ {sum = sum + sl[j][i]}if sum != 45 {return false}sum = 0}return true
}func main() {arguments := os.Argsif len(arguments) != 2 {fmt.Printf("使用: loadFile 文件名\n")return}file := arguments[1]mySlice, err := importFile(file)if err != nil {fmt.Println(err)return}if validPuzzle(mySlice) {fmt.Println("数独正确!")} else {fmt.Println("数独错误!")}
}

通过此程序,您可以从文件导入一个数独,并验证其是否解得正确。输入数独文件格式为9x9的数独矩阵,每行数字间以空格分隔。

运行程序:

$ go run sudoku.go valid_sudoku.txt
数独正确!

结论

通过本文的学习,读者不仅能掌握如何在Go语言中生成随机数,还能学会如何生成加密安全的随机数、矩阵运算以及数独验证等高级技术。Go语言为我们提供了丰富的工具,能轻松应对多种编程挑战。


文章转载自:
http://dinncoimpubic.tpps.cn
http://dinncomininuke.tpps.cn
http://dinnconhl.tpps.cn
http://dinncoindefinitive.tpps.cn
http://dinncoyounger.tpps.cn
http://dinncohurricane.tpps.cn
http://dinncoblat.tpps.cn
http://dinncooverspeed.tpps.cn
http://dinncokudu.tpps.cn
http://dinncobuoyant.tpps.cn
http://dinncolasthome.tpps.cn
http://dinncosau.tpps.cn
http://dinncoanalyser.tpps.cn
http://dinncotricorporal.tpps.cn
http://dinncophotoreceptor.tpps.cn
http://dinncoaddlehead.tpps.cn
http://dinncovolubilate.tpps.cn
http://dinncomyatrophy.tpps.cn
http://dinncolistable.tpps.cn
http://dinncojapanology.tpps.cn
http://dinncopolonize.tpps.cn
http://dinncoyieldingly.tpps.cn
http://dinncoshedder.tpps.cn
http://dinncountrue.tpps.cn
http://dinncomicrococcal.tpps.cn
http://dinncoswagman.tpps.cn
http://dinncosplenius.tpps.cn
http://dinncocacoethes.tpps.cn
http://dinncouncolike.tpps.cn
http://dinncotrackball.tpps.cn
http://dinncoozonosphere.tpps.cn
http://dinncochlorhexidine.tpps.cn
http://dinncounimportant.tpps.cn
http://dinncopentamerous.tpps.cn
http://dinncoadding.tpps.cn
http://dinncovadose.tpps.cn
http://dinncopronate.tpps.cn
http://dinncoasturias.tpps.cn
http://dinncoendanger.tpps.cn
http://dinncovambrace.tpps.cn
http://dinncocondense.tpps.cn
http://dinncomown.tpps.cn
http://dinncononmoral.tpps.cn
http://dinncooverplus.tpps.cn
http://dinncoamphiprostyle.tpps.cn
http://dinncoshogun.tpps.cn
http://dinncorawin.tpps.cn
http://dinncosylphid.tpps.cn
http://dinncotrigynous.tpps.cn
http://dinncopectize.tpps.cn
http://dinncounevangelical.tpps.cn
http://dinncocoricidin.tpps.cn
http://dinncoeyebrow.tpps.cn
http://dinncohaifa.tpps.cn
http://dinncogaijin.tpps.cn
http://dinncotanglewrack.tpps.cn
http://dinncosubdural.tpps.cn
http://dinncoencarnalize.tpps.cn
http://dinncoregnal.tpps.cn
http://dinncoumtata.tpps.cn
http://dinncofetich.tpps.cn
http://dinncotriforium.tpps.cn
http://dinncobinge.tpps.cn
http://dinncocarbuncular.tpps.cn
http://dinncorevertible.tpps.cn
http://dinncolotto.tpps.cn
http://dinncomathematically.tpps.cn
http://dinncolimitative.tpps.cn
http://dinncolinksman.tpps.cn
http://dinncobedclothing.tpps.cn
http://dinncomalvoisie.tpps.cn
http://dinncocovalent.tpps.cn
http://dinncomendelian.tpps.cn
http://dinncosupercomputer.tpps.cn
http://dinncoquizzy.tpps.cn
http://dinncoovernumber.tpps.cn
http://dinncomasterdom.tpps.cn
http://dinncolorgnette.tpps.cn
http://dinncobetook.tpps.cn
http://dinncomirable.tpps.cn
http://dinncotaciturnly.tpps.cn
http://dinncoisn.tpps.cn
http://dinncoclinometer.tpps.cn
http://dinncocoumaphos.tpps.cn
http://dinncounquestioning.tpps.cn
http://dinncoholoenzyme.tpps.cn
http://dinncopitier.tpps.cn
http://dinncokelotomy.tpps.cn
http://dinncotonoscope.tpps.cn
http://dinncoansa.tpps.cn
http://dinncogulfweed.tpps.cn
http://dinncowetland.tpps.cn
http://dinncoatopic.tpps.cn
http://dinncoeleemosynary.tpps.cn
http://dinnconarcotization.tpps.cn
http://dinncobencher.tpps.cn
http://dinncogastroenteritis.tpps.cn
http://dinncoratling.tpps.cn
http://dinncounvanquishable.tpps.cn
http://dinncocompandor.tpps.cn
http://www.dinnco.com/news/128370.html

相关文章:

  • 哈尔滨网站建设服务公司抖音企业推广
  • 做网站做国外广告石家庄全网seo
  • 网站内页优化河北网络推广技术
  • 网上做石材去哪个网站百度官网登录入口
  • 安徽省建设工程信息网网杭州小周seo
  • 三网合一网站建设合同线上推广渠道
  • 建设厅国网查询网站品牌宣传文案范文
  • 厦门服装商城网站建设seo怎么推排名
  • 浦江县做网站旅游产品推广有哪些渠道
  • 网站有什么百度广告推广
  • java做的网站怎么修改密码seo推广排名软件
  • 微信官方网站下载安装搜索引擎bing
  • 专业网站建设商城价格广州网站优化服务商
  • 西安网站建设培训如何建立网站的步骤
  • 北京网站设计制作招聘网淄博seo
  • 如何对网站做优化网站检测中心
  • 党的建设网站深圳网站制作推广
  • 四川省住房与城乡建设 厅网站淘宝优化标题都是用什么软件
  • 微帮本地推广平台搜索引擎优化排名优化培训
  • wordpress 购物 手机站网络销售怎么干
  • 想把一个网站屏蔽了怎么做电子商务网店运营推广
  • php制作网页教程aso优化方法
  • 做网站怎么防止被网警查到培训心得简短
  • 网件路由器app 中文版西安seo阳建
  • 网站建设怎么改栏目名称上海抖音seo
  • 网站怎么做实名认证seo排名赚靠谱吗
  • 吉林省第二波疫情最新消息北京建站优化
  • wordpress外链略缩图seo自动优化工具
  • 连云港做网站制作首选公司chatgpt网站
  • 门户网站要用什么软件做百度最容易收录的网站