aboutsummaryrefslogtreecommitdiff
path: root/plugin/pkg/rand/rand.go
blob: 490f59b6dd04c2f20653db4a1f159fb3f2930a79 (plain) (blame)
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
// Package rand is used for concurrency safe random number generator.
package rand

import (
	"math/rand"
	"sync"
)

// Rand is used for concurrency safe random number generator.
type Rand struct {
	m sync.Mutex
	r *rand.Rand
}

// New returns a new Rand from seed.
func New(seed int64) *Rand {
	return &Rand{r: rand.New(rand.NewSource(seed))}
}

// Int returns a non-negative pseudo-random int from the Source in Rand.r.
func (r *Rand) Int() int {
	r.m.Lock()
	v := r.r.Int()
	r.m.Unlock()
	return v
}

// Perm returns, as a slice of n ints, a pseudo-random permutation of the
// integers in the half-open interval [0,n) from the Source in Rand.r.
func (r *Rand) Perm(n int) []int {
	r.m.Lock()
	v := r.r.Perm(n)
	r.m.Unlock()
	return v
}