当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


GO Intn用法及代码示例


GO语言"math/rand"包中"Intn"函数的用法及代码示例。

用法:

func Intn(n int) int

Intn 从默认 Source 返回半开区间 [0,n) 中的非负伪随机数,作为 int。如果 n <= 0,它会Panics。

例子:

package main

import (
	"fmt"
	"math/rand"
)

func main() {
	// Seeding with the same value results in the same random sequence each run.
	// For different numbers, seed with a different value, such as
	// time.Now().UnixNano(), which yields a constantly-changing number.
	rand.Seed(86)
	fmt.Println(rand.Intn(100))
	fmt.Println(rand.Intn(100))
	fmt.Println(rand.Intn(100))

}

输出:

42
76
30

相关用法


注:本文由纯净天空筛选整理自golang.google.cn大神的英文原创作品 Intn。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。