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

Golang 实现 10 进制转 N 进制

给定一个不没有重复字符的字符串,如 `[0-9,a-z]`,把一个 10 进制数字转为,该字符集的字符串。应用场合如汽车牌、顺序计数。...

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

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