2024-02-18 10:42:21 +00:00
|
|
|
package rand
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Int returns a non-negative pseudo-random int.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
func Int() int { return pseudo.Int() }
|
|
|
|
|
|
|
|
// Intn returns, as an int, a non-negative pseudo-random number in [0,n).
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
// It panics if n <= 0.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
func Intn(n int) int { return pseudo.Intn(n) }
|
|
|
|
|
|
|
|
// Int63n returns, as an int64, a non-negative pseudo-random number in [0,n).
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
// It panics if n <= 0.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
func Int63n(n int64) int64 { return pseudo.Int63n(n) }
|
|
|
|
|
|
|
|
// Perm returns, as a slice of n ints, a pseudo-random permutation of the integers [0,n).
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
func Perm(n int) []int { return pseudo.Perm(n) }
|
|
|
|
|
|
|
|
// Seed uses the provided seed value to initialize the default Source to a
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
// deterministic state. If Seed is not called, the generator behaves as if
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
// seeded by Seed(1).
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
func Seed(n int64) { pseudo.Seed(n) }
|
|
|
|
|
|
|
|
var pseudo = rand.New(&source{src: rand.NewSource(1)})
|
|
|
|
|
|
|
|
type source struct {
|
|
|
|
src rand.Source
|
2024-07-24 23:45:04 +00:00
|
|
|
|
|
|
|
mu sync.Mutex
|
2024-02-18 10:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *source) Int63() int64 {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
s.mu.Lock()
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
n := s.src.Int63()
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
s.mu.Unlock()
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
return n
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *source) Seed(seed int64) {
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
s.mu.Lock()
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
s.src.Seed(seed)
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
s.mu.Unlock()
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Shuffle pseudo-randomizes the order of elements.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
// n is the number of elements.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
// swap swaps the elements with indexes i and j.
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-02-18 10:42:21 +00:00
|
|
|
func Shuffle(n int, swap func(i, j int)) { pseudo.Shuffle(n, swap) }
|