首页 > 其他分享 >0129-Go-随机数

0129-Go-随机数

时间:2022-11-28 22:36:02浏览次数:73  
标签:Println rand fmt Intn 0129 Print 随机数 Go 100

环境

  • Time 2022-08-25
  • Go 1.19

前言

说明

参考:https://gobyexample.com/random-numbers

目标

使用 Go 语言的随机数。

示例

package main

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

func main() {

    fmt.Print(rand.Intn(100), ",")
    fmt.Print(rand.Intn(100))
    fmt.Println()

    fmt.Println(rand.Float64())

    fmt.Print((rand.Float64()*5)+5, ",")
    fmt.Print((rand.Float64() * 5) + 5)
    fmt.Println()

    s1 := rand.NewSource(time.Now().UnixNano())
    r1 := rand.New(s1)

    fmt.Print(r1.Intn(100), ",")
    fmt.Print(r1.Intn(100))
    fmt.Println()

    s2 := rand.NewSource(42)
    r2 := rand.New(s2)
    fmt.Print(r2.Intn(100), ",")
    fmt.Print(r2.Intn(100))
    fmt.Println()
    s3 := rand.NewSource(42)
    r3 := rand.New(s3)
    fmt.Print(r3.Intn(100), ",")
    fmt.Print(r3.Intn(100))
}

总结

使用 Go 语言的随机数。

附录

标签:Println,rand,fmt,Intn,0129,Print,随机数,Go,100
From: https://www.cnblogs.com/jiangbo4444/p/16933865.html

相关文章

  • 0131-Go-URL 解析
    环境Time2022-08-25Go1.19前言说明参考:https://gobyexample.com/url-parsing目标使用Go语言的URL解析。示例packagemainimport("fmt""net"......
  • 0132-Go-SHA256
    环境Time2022-08-25Go1.19前言说明参考:https://gobyexample.com/sha256-hashes目标使用Go语言的SHA256。示例packagemainimport("crypto/sha256"......
  • 0133-Go-Base64
    环境Time2022-08-25Go1.19前言说明参考:https://gobyexample.com/base64-encoding目标使用Go语言的Base64。示例packagemainimport(b64"encoding......
  • 0121-Go-字符串格式化
    环境Time2022-08-25Go1.19前言说明参考:https://gobyexample.com/string-formatting目标使用Go语言的字符串格式化。示例packagemainimport("fmt"......
  • 0120-Go-字符串函数
    环境Time2022-08-25Go1.19前言说明参考:https://gobyexample.com/string-functions目标使用Go语言的字符串函数。示例packagemainimport("fmt"......
  • 0122-Go-模板字符串
    环境Time2022-08-25Go1.19前言说明参考:https://gobyexample.com/text-templates目标使用Go语言的模板字符串。示例packagemainimport("os""t......
  • 0123-Go-正则表达式
    环境Time2022-08-25Go1.19前言说明参考:https://gobyexample.com/regular-expressions目标使用Go语言的正则表达式。示例packagemainimport( "bytes" ......
  • 0124-Go-JSON 转换
    环境Time2022-08-25Go1.19前言说明参考:https://gobyexample.com/json目标使用Go语言的JSON。简单值packagemainimport("encoding/json""fmt......
  • 0125-Go-XML 转换
    环境Time2022-08-25Go1.19前言说明参考:https://gobyexample.com/xml目标使用Go语言的XML。示例packagemainimport("encoding/xml""fmt")......
  • go zap日志库
    https://github.com/labring/sealos/blob/main/pkg/utils/logger/logger.gopackagelogger   import( "fmt" "os" "strings"......