Source File
rand.go
Belonging Package
google.golang.org/protobuf/internal/detrand
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package detrand provides deterministically random functionality.
//
// The pseudo-randomness of these functions is seeded by the program binary
// itself and guarantees that the output does not change within a program,
// while ensuring that the output is unstable across different builds.
package detrand
import (
)
// Disable disables detrand such that all functions returns the zero value.
// This function is not concurrent-safe and must be called during program init.
func () {
randSeed = 0
}
// Bool returns a deterministically random boolean.
func () bool {
return randSeed%2 == 1
}
// Intn returns a deterministically random integer between 0 and n-1, inclusive.
func ( int) int {
if <= 0 {
panic("must be positive")
}
return int(randSeed % uint64())
}
// randSeed is a best-effort at an approximate hash of the Go binary.
var randSeed = binaryHash()
func () uint64 {
// Open the Go binary.
, := os.Executable()
if != nil {
return 0
}
, := os.Open()
if != nil {
return 0
}
defer .Close()
// Hash the size and several samples of the Go binary.
const = 8
var [64]byte
:= fnv.New64()
, := .Stat()
if != nil {
return 0
}
binary.LittleEndian.PutUint64([:8], uint64(.Size()))
.Write([:8])
for := int64(0); < ; ++ {
if , := .ReadAt([:], *.Size()/); != nil {
return 0
}
.Write([:])
}
return .Sum64()
}
The pages are generated with Golds v0.4.9. (GOOS=linux GOARCH=amd64)