一文帶你了解Go語言標準庫math和rand的常用函數(shù)
math 標準庫
math
標準庫提供了一些 常量如 int64
類型的最大值、float64
類型的最大值等,和常用的數(shù)學(xué)計算函數(shù)。
常用函數(shù)
函數(shù) | 說明 |
---|---|
Abs(x float64) float64 | 傳入 x 參數(shù),返回 x 的絕對值 |
Max(x, y float64) float64 | 傳入 x、y 參數(shù),返回 x 與 y 中的最大值 |
Min(x, y float64) float64 | 傳入 x、y 參數(shù),返回 x 和 y 中的最小值 |
Ceil(x float64) float64 | 傳入 x 參數(shù),返回一個大于等于 x 的最小整數(shù)值,也就是向上取整 |
Ceil(x float64) float64 | 傳入 x 參數(shù),返回一個小于等于 x 的最小整數(shù)值,也就是向下取整 |
Trunc(x float64) float64 | 傳入 x 參數(shù),返回浮點數(shù) x 的整數(shù)部分,使用 Floor 函數(shù)也能實現(xiàn) |
Dim(x, y float64) float64 | 傳入 x、y 參數(shù),返回 x-y 與 0 中最大的值 |
Mod(x, y float64) float64 | 對 x / y 進行取余運算 x % y |
Pow(x, y float64) float64 | 計算 x 的 y 次冪 |
Sqrt(x float64) float64 | 對 x 開平方 |
Cbrt(x float64) float64 | 對 x 開立方 |
Modf(f float64) (int float64, frac float64) | 分別取出 f 的整數(shù)部分和小數(shù)部分 |
如果想了解更多函數(shù)介紹和使用,可以到 pkg.go.dev/math 進行查看。
Abs 函數(shù)
Abs(x float64) float64
:返回 x
的絕對值。 示例:
import ( "fmt" "math" ) func main() { fmt.Println(math.Abs(-3.14)) // 3.14 }
Max 函數(shù)
Max(x, y float64) float64
:返回 x
與 y
中的最大值。 示例:
import ( "fmt" "math" ) func main() { fmt.Println(math.Max(1.5, 2.5)) // 2.5 }
Min 函數(shù)
Min(x, y float64) float64
:返回 x
和 y
中的最小值。 示例:
import ( "fmt" "math" ) func main() { fmt.Println(math.Min(1.5, 2.5)) // 1.5 }
Ceil
Ceil(x float64) float64
:返回一個大于等于 x
的最小整數(shù)值,也就是向上取整。 示例:
import ( "fmt" "math" ) func main() { fmt.Println(math.Ceil(1.4)) // 2 fmt.Println(math.Ceil(2)) // 2 }
Floor 函數(shù)
Ceil(x float64) float64
:返回一個小于等于 x
的最小整數(shù)值,也就是向下取整。 示例:
import ( "fmt" "math" ) func main() { fmt.Println(math.Floor(1.4)) // 1 fmt.Println(math.Floor(1)) // 1 }
Trunc 函數(shù)
Trunc(x float64) float64
:返回浮點數(shù) x
的整數(shù)部分,使用 Floor
函數(shù)也能實現(xiàn)。 示例:
import ( "fmt" "math" ) func main() { fmt.Println(math.Trunc(1.4)) // 1 }
Dim 函數(shù)
Dim(x, y float64) float64
:返回 x-y
與 0
中最大的值。 示例:
import ( "fmt" "math" ) func main() { fmt.Println(math.Dim(2.0, 1.0)) // 1 fmt.Println(math.Dim(1.0, 2.0)) // 0 }
Mod 函數(shù)
Mod(x, y float64) float64
:對 x / y
進行取余運算 x % y
。 示例:
import ( "fmt" "math" ) func main() { fmt.Println(math.Mod(5.0, 3.0)) // 3 fmt.Println(math.Mod(3.0, 3.0)) // 0 }
Pow 函數(shù)
Pow(x, y float64) float64
:計算 x
的 y
次冪。 示例:
import ( "fmt" "math" ) func main() { fmt.Println(math.Pow(2, 1)) // 2 fmt.Println(math.Pow(2, 5)) // 32 }
Sqrt 函數(shù)
Sqrt(x float64) float64
:對 x
開平方。 示例:
import ( "fmt" "math" ) func main() { fmt.Println(math.Sqrt(64)) // 8 fmt.Println(math.Sqrt(16)) // 4 }
Cbrt 函數(shù)
Cbrt(x float64) float64
:對 x
開立方。 示例:
import ( "fmt" "math" ) func main() { fmt.Println(math.Cbrt(64)) // 4 fmt.Println(math.Cbrt(8)) // 2 }
Modf 函數(shù)
Modf(f float64) (int float64, frac float64)
:分別取出 f
的整數(shù)部分和小數(shù)部分。
int
參數(shù),整數(shù)部分。frac
參數(shù),小數(shù)部分。 示例:
import ( "fmt" "math" ) func main() { integer, decimal := math.Modf(3.1415) fmt.Printf("整數(shù)部分:%.f 小數(shù)部分:%.4f", integer, decimal) // 整數(shù)部分:3 小數(shù)部分:0.1415 }
rand
rand
標準庫提供了多個獲取不同類型隨機數(shù)的函數(shù)。
常用函數(shù)
函數(shù) | 說明 |
---|---|
Int() int | 返回一個 int 類型的非負的偽隨機數(shù) |
Intn(n int) int | 返回一個 0 到 n 中(不包括 n)的 int 類型的非負偽隨機數(shù) |
Int31() int32 | 返回一個 int32 類型的非負的偽隨機數(shù) |
Uint32() uint32 | 返回一個 uint32 類型的非負的偽隨機數(shù) |
Int31n(n int32) int32 | 返回一個 0 到 n 中(不包括 n)的 int32 類型的非負偽隨機數(shù) |
Int63() int64 | 返回一個 int64 類型的非負的偽隨機數(shù) |
Uint64() uint64 | 返回一個 uint64 類型的非負的偽隨機數(shù) |
Int63n(n int64) int64 | 返回一個 0 到 n 中(不包括 n)的 int64 類型的非負偽隨機數(shù) |
Float32() float32 | 返回一個 0.0 到 1.0 中(不包括 1.0)的 float32 類型的非負偽隨機數(shù) |
Float64() float64 | 返回一個 0.0 到 1.0 中(不包括 1.0)的 float64 類型的非負偽隨機數(shù) |
Seed(seed int64) | 設(shè)置隨機種子,使得每次獲取隨機數(shù)的值都不一樣 |
如果想了解更多函數(shù)介紹和使用,可以到 pkg.go.dev/math/rand 進行查看。
代碼示例
import ( "fmt" "math/rand" ) func main() { fmt.Println(rand.Int()) // 5577006791947779410 fmt.Println(rand.Intn(10)) // 7 fmt.Println(rand.Int31()) // 1427131847 fmt.Println(rand.Uint32()) // 1879968118 fmt.Println(rand.Int31n(10)) // 1 fmt.Println(rand.Int63()) // 6334824724549167320 fmt.Println(rand.Uint64()) // 9828766684487745566 fmt.Println(rand.Int63n(10)) // 8 fmt.Println(rand.Float32()) // 0.09696952 fmt.Println(rand.Float64()) // 0.30091186058528707 }
多次運行上述代碼,發(fā)現(xiàn)獲取到的隨機數(shù)都是一樣的,這是因為我們沒有設(shè)置隨機種子。可以通過 Seed
函數(shù)進行設(shè)置:
import ( "fmt" "math/rand" "time" ) func main() { rand.Seed(time.Now().Unix()) fmt.Println(rand.Intn(10)) }
使用 Seed
函數(shù)設(shè)置隨機種子,將當前時間的秒數(shù)作為參數(shù)。后續(xù)多次獲取隨機數(shù)的值將不會一直一樣。
小結(jié)
本文介紹了標準庫 math
和 rand
的常用函數(shù)的用法,并通過例子進行說明。
math
庫里雖說有最大值和最小值比較,但是形參類型必須是浮點型,如果我們想比較的是整型的最大最小值,就得自己封裝函數(shù)。
獲取隨機數(shù)時,不要忘記設(shè)置隨機種子,否則多次獲取到的隨機數(shù)將會是一樣的。
以上就是一文帶你了解Go語言標準庫math和rand的常用函數(shù)的詳細內(nèi)容,更多關(guān)于Go語言math rand的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
一文詳解Golang中net/http包的實現(xiàn)原理
這篇文章主要介紹了如何用?net/http?自己編寫實現(xiàn)一個?HTTP?Server?并探究其實現(xiàn)原理,具體講解Go語言是如何接收和處理請求的,希望能夠?qū)Υ蠹业膶W(xué)習(xí)或工作具有一定的幫助2022-08-08