package main import ( "math/rand" "time" "fmt" ) func InitData(p *int) { rand.Seed(time.Now().UnixNano()) var num int for { num = rand.Intn(10000) if num >= 1000 { break } } *p = num } func getNum(s []int, n int) { s[0] = n / 1000 //这里为取千位 s[1] = n % 1000 / 100 //取百位 s[2] = n % 100 / 10 //取十位 s[3] = n % 10 //取个位 } func main() { var num int InitData(&num) fmt.Printf("random num is %d\n", num) randslice := make([]int, 4) getNum(randslice, num) fmt.Printf("slice = %v\n", randslice) }
执行结果
random num is 2261 slice = [2 2 6 1]
原文地址:https://www.cnblogs.com/baylorqu/p/9589350.html
时间: 2024-10-31 10:52:34