-
Notifications
You must be signed in to change notification settings - Fork 0
/
rand.go
77 lines (67 loc) · 2.38 KB
/
rand.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright 2014 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// Author: Ben Darnell
package randutil
import (
crypto_rand "crypto/rand"
"encoding/binary"
"fmt"
"log" // Don't bring cockroach/util/log into this low-level package.
"math/rand"
"github.com/cockroachdb/cockroach/pkg/util/envutil"
)
// NewPseudoSeed generates a seed from crypto/rand.
func NewPseudoSeed() int64 {
var seed int64
err := binary.Read(crypto_rand.Reader, binary.LittleEndian, &seed)
if err != nil {
panic(fmt.Sprintf("could not read from crypto/rand: %s", err))
}
return seed
}
// NewPseudoRand returns an instance of math/rand.Rand seeded from crypto/rand
// and its seed so we can easily and cheaply generate unique streams of
// numbers. The created object is not safe for concurrent access.
func NewPseudoRand() (*rand.Rand, int64) {
seed := NewPseudoSeed()
return rand.New(rand.NewSource(seed)), seed
}
// RandIntInRange returns a value in [min, max)
func RandIntInRange(r *rand.Rand, min, max int) int {
return min + r.Intn(max-min)
}
var randLetters = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
// RandBytes returns a byte slice of the given length with random
// data.
func RandBytes(r *rand.Rand, size int) []byte {
if size <= 0 {
return nil
}
arr := make([]byte, size)
for i := 0; i < len(arr); i++ {
arr[i] = randLetters[r.Intn(len(randLetters))]
}
return arr
}
// SeedForTests seeds the random number generator and prints the seed
// value used. This value can be specified via an environment variable
// COCKROACH_RANDOM_SEED=x to reuse the same value later. This function should
// be called from TestMain; individual tests should not touch the seed
// of the global random number generator.
func SeedForTests() {
seed := envutil.EnvOrDefaultInt64("COCKROACH_RANDOM_SEED", NewPseudoSeed())
rand.Seed(seed)
log.Printf("Random seed: %v", seed)
}