ZhangZhihui's Blog  

The purpose of the /dev/random system device is to generate random data, which you might use to test your programs or, in this case, as the seed for a random number generator.

 

package main

import (
    "encoding/binary"
    "fmt"
    "os"
)

func main() {
    f, err := os.Open("/dev/random")
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    defer f.Close()

    var seed int64
    binary.Read(f, binary.LittleEndian, &seed)
    fmt.Println("Seed:", seed)
}

 

zzh@ZZHPC:/zdata/Github/ztest$ go run main.go
Seed: 4931188275749169713
zzh@ZZHPC:/zdata/Github/ztest$ go run main.go
Seed: 4824298121626473918
zzh@ZZHPC:/zdata/Github/ztest$ go run main.go
Seed: -8967568002750916215
zzh@ZZHPC:/zdata/Github/ztest$ go run main.go
Seed: 261928363319546217
zzh@ZZHPC:/zdata/Github/ztest$ go run main.go
Seed: 3557688354067943356
zzh@ZZHPC:/zdata/Github/ztest$ go run main.go
Seed: -8635101054262113742
zzh@ZZHPC:/zdata/Github/ztest$ go run main.go
Seed: -5691620843952660021

 

posted on 2024-06-12 14:13  ZhangZhihuiAAA  阅读(5)  评论(0编辑  收藏  举报