Golang 生成随机数

package main

import (
	"encoding/binary"
	"fmt"
	"hash/fnv"
	"math/rand"
	"net"
	"time"
)

func getMacAddr() (string, error) {
	netInterfaces, error := net.Interfaces()
	if error != nil {
		return "", error
	}
	for _, netInterfaces := range netInterfaces {
		macaddr := netInterfaces.HardwareAddr.String()
		if len(macaddr) == 0 {
			continue
		}
		return macaddr, error
	}
	return "", error
}

func Hash(s string) uint32 {
	h := fnv.New32a()
	h.Write([]byte(s))
	return h.Sum32()
}

func main() {
	var t = time.Now().Unix()
	macaddr, _ := getMacAddr()
	tMacaddr := fmt.Sprintf("%d %s", t, macaddr)
	tMacaddrhashed := Hash(tMacaddr)
	int64var := int64(tMacaddrhashed)
	rand.Seed(int64var)
	rnd := make([]byte, 4)
	binary.BigEndian.PutUint32(rnd, rand.Uint32())
	fmt.Printf("rnd is %d", rnd)
}

 

你可能感兴趣的:(Golang,编程,脚本,随机数,Go,Golang)