GolangNote

Golang笔记

Golang 获取两个数之间的随机数

Permalink

Golang 获取两个数之间的随机数,下面的例子可以随机生成4 位数字的验证码

Go: 获取两个数之间的随机数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package main

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

func random(min, max int) int {
	rand.Seed(time.Now().Unix())
	return rand.Intn(max-min) + min
}

func main() {
	myrand := random(1000, 9999)
	fmt.Println(myrand)
}

本文网址: https://golangnote.com/topic/152.html 转摘请注明来源

Related articles

Write a Comment to "Golang 获取两个数之间的随机数"

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.22.3 Processed in 0ms