問題解決方法:
在這裏,我們將演示 rand.Intn() 函數的使用。 rand.Intn() 函數返回 0-N 之間的隨機數。這裏,N 是函數 rand.Intn() 中指定的數字。
程序/源代碼:
下麵給出了演示 rand.Intn() 函數的源代碼。給定的程序在 ubuntu 18.04 操作係統上編譯和執行成功。
// Golang program to demonstrate the
// rand.Intn() function
package main
import "math/rand"
import "fmt"
//Entry point for the program
func main() {
fmt.Println("Random number:", rand.Intn(786))
fmt.Println("Random number:", rand.Intn(786))
fmt.Println("Random number:", rand.Intn(786))
fmt.Println("Random number:", rand.Intn(786))
}
輸出:
RUN 1: Random number: 143 Random number: 279 Random number: 293 Random number: 371
說明:
在上麵的程序中,我們聲明了包 main。 main 包用於告訴 Go 語言編譯器必須編譯該包並生成可執行文件。在這裏,我們將所需的包導入到預定義的函數中。
在 main() 函數中,我們四次使用了參數值為 786 的 rand.Intn() 函數。 rand.Intn() 函數返回 0-786 之間的隨機數。之後,我們在控製台屏幕上打印結果。
相關用法
- Golang rand.Seed()用法及代碼示例
- Golang reflect.Kind()用法及代碼示例
- Golang reflect.FuncOf()用法及代碼示例
- Golang reflect.NumMethod()用法及代碼示例
- Golang ring.Len()用法及代碼示例
- Golang reflect.Complex()用法及代碼示例
- Golang reflect.Int()用法及代碼示例
- Golang reflect.ArrayOf()用法及代碼示例
- Golang reflect.Close()用法及代碼示例
- Golang reflect.IsNil()用法及代碼示例
- Golang reflect.DeepEqual()用法及代碼示例
- Golang reflect.MapIndex()用法及代碼示例
- Golang reflect.Len()用法及代碼示例
- Golang reflect.Tag.Lookup()用法及代碼示例
- Golang reflect.Set()用法及代碼示例
- Golang reflect.NewAt()用法及代碼示例
- Golang reflect.MakeMap()用法及代碼示例
- Golang reflect.CanAddr()用法及代碼示例
- Golang reflect.FieldByIndex()用法及代碼示例
- Golang reflect.SetBool()用法及代碼示例
注:本文由純淨天空篩選整理自 Golang program to demonstrate the rand.Intn() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。