go的随机数

go的随机数,大多时候要用rand.Seed(time.Now().UnixNano())才会有变化,但是此次不用也没事,具体详情没搞清楚

package main

import (
	"fmt"
	"math/rand"
	"time"
)

func init() {
	rand.Seed(time.Now().UnixNano())
}

func main() {
	for i := 0; i < 10; i++ {
		fmt.Println(rand.Int())
	}
	fmt.Println()

	for i := 0; i < 10; i++ {
		fmt.Println(rand.Intn(100))
	}
	fmt.Println()

	for i := 0; i < 10; i++ {
		fmt.Println(rand.Float64())
	}
	fmt.Println()

	str1 := "hello"
	str2 := "world"
	//str3 := str1 + " " + str2
	str4 := fmt.Sprintf("%s %s", str1, str2)
	n := len(str4)
	str5 := str4[4:5]
	fmt.Println(str5)
	println(n)
	tmp := []byte(str4)
	fmt.Println(len(tmp))
}

posted @ 2022-02-26 23:42  ty1539  阅读(58)  评论(0编辑  收藏  举报