當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。