1
// Copyright 2018 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
5
// Package detrand provides deterministically random functionality.
7
// The pseudo-randomness of these functions is seeded by the program binary
8
// itself and guarantees that the output does not change within a program,
9
// while ensuring that the output is unstable across different builds.
18
// Disable disables detrand such that all functions returns the zero value.
19
// This function is not concurrent-safe and must be called during program init.
24
// Bool returns a deterministically random boolean.
26
return randSeed%2 == 1
29
// Intn returns a deterministically random integer between 0 and n-1, inclusive.
32
panic("must be positive")
34
return int(randSeed % uint64(n))
37
// randSeed is a best-effort at an approximate hash of the Go binary.
38
var randSeed = binaryHash()
40
func binaryHash() uint64 {
41
// Open the Go binary.
42
s, err := os.Executable()
52
// Hash the size and several samples of the Go binary.
60
binary.LittleEndian.PutUint64(buf[:8], uint64(fi.Size()))
62
for i := int64(0); i < numSamples; i++ {
63
if _, err := f.ReadAt(buf[:], i*fi.Size()/numSamples); err != nil {