示例代码如下:

package main

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

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

使用rand.Seed()函数设置随机数种子,确保每次运行程序生成的随机数不同。使用rand.Intn()函数生成1到1000之间的随机数。

golang 120版本在11000中随机

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

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