生成一个整数:

package main

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

func main() {
    rand.Seed(time.Now().UnixNano())
    num := rand.Intn(1000-1) + 1
    fmt.Println(num)
}

这里使用了 rand.Intn() 函数来生成 1 到 1000 之间的随机整数,并使用当前时间作为随机数生成器的种子。

golang 在11000中随机

原文地址: https://www.cveoy.top/t/topic/bwKc 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录