GO随机数生成

我们可以使用rand对象生成随机数。我们应该为rand对象提供一些种子, 以使生成的数量不同。如果我们不提供种子, 那么编译器将始终产生相同的结果。

去随机数生成

package main

import "fmt"
import (
	"math/rand"
	//"time"
	"time"
)
func main() {
	fmt.Print(rand.Intn(100))  //will produce random integer between 0 to 100
	fmt.Println()

	fmt.Print(rand.Float64())	// will produce random number between 0 to 1
	fmt.Println()
	
	rand.Seed(time.Now().Unix())  // seeding do that random number will produced
	myrand := random(1, 20)

	fmt.Println(myrand)

}

func random(min, max int) int {
	return rand.Intn(max - min) + min
}

输出:

81
0.9405090880450124
17
微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_50009.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?