github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/src/pkg/math/rand/rand.go (about) 1 // Copyright 2009 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. 4 5 // Package rand implements pseudo-random number generators. 6 // 7 // Random numbers are generated by a Source. Top-level functions, such as 8 // Float64 and Int, use a default shared Source that produces a deterministic 9 // sequence of values each time a program is run. Use the Seed function to 10 // initialize the default Source if different behavior is required for each run. 11 // The default Source is safe for concurrent use by multiple goroutines. 12 package rand 13 14 import "sync" 15 16 // A Source represents a source of uniformly-distributed 17 // pseudo-random int64 values in the range [0, 1<<63). 18 type Source interface { 19 Int63() int64 20 Seed(seed int64) 21 } 22 23 // NewSource returns a new pseudo-random Source seeded with the given value. 24 func NewSource(seed int64) Source { 25 var rng rngSource 26 rng.Seed(seed) 27 return &rng 28 } 29 30 // A Rand is a source of random numbers. 31 type Rand struct { 32 src Source 33 } 34 35 // New returns a new Rand that uses random values from src 36 // to generate other random values. 37 func New(src Source) *Rand { return &Rand{src} } 38 39 // Seed uses the provided seed value to initialize the generator to a deterministic state. 40 func (r *Rand) Seed(seed int64) { r.src.Seed(seed) } 41 42 // Int63 returns a non-negative pseudo-random 63-bit integer as an int64. 43 func (r *Rand) Int63() int64 { return r.src.Int63() } 44 45 // Uint32 returns a pseudo-random 32-bit value as a uint32. 46 func (r *Rand) Uint32() uint32 { return uint32(r.Int63() >> 31) } 47 48 // Int31 returns a non-negative pseudo-random 31-bit integer as an int32. 49 func (r *Rand) Int31() int32 { return int32(r.Int63() >> 32) } 50 51 // Int returns a non-negative pseudo-random int. 52 func (r *Rand) Int() int { 53 u := uint(r.Int63()) 54 return int(u << 1 >> 1) // clear sign bit if int == int32 55 } 56 57 // Int63n returns, as an int64, a non-negative pseudo-random number in [0,n). 58 // It panics if n <= 0. 59 func (r *Rand) Int63n(n int64) int64 { 60 if n <= 0 { 61 panic("invalid argument to Int63n") 62 } 63 max := int64((1 << 63) - 1 - (1<<63)%uint64(n)) 64 v := r.Int63() 65 for v > max { 66 v = r.Int63() 67 } 68 return v % n 69 } 70 71 // Int31n returns, as an int32, a non-negative pseudo-random number in [0,n). 72 // It panics if n <= 0. 73 func (r *Rand) Int31n(n int32) int32 { 74 if n <= 0 { 75 panic("invalid argument to Int31n") 76 } 77 max := int32((1 << 31) - 1 - (1<<31)%uint32(n)) 78 v := r.Int31() 79 for v > max { 80 v = r.Int31() 81 } 82 return v % n 83 } 84 85 // Intn returns, as an int, a non-negative pseudo-random number in [0,n). 86 // It panics if n <= 0. 87 func (r *Rand) Intn(n int) int { 88 if n <= 0 { 89 panic("invalid argument to Intn") 90 } 91 if n <= 1<<31-1 { 92 return int(r.Int31n(int32(n))) 93 } 94 return int(r.Int63n(int64(n))) 95 } 96 97 // Float64 returns, as a float64, a pseudo-random number in [0.0,1.0). 98 func (r *Rand) Float64() float64 { return float64(r.Int63()) / (1 << 63) } 99 100 // Float32 returns, as a float32, a pseudo-random number in [0.0,1.0). 101 func (r *Rand) Float32() float32 { return float32(r.Float64()) } 102 103 // Perm returns, as a slice of n ints, a pseudo-random permutation of the integers [0,n). 104 func (r *Rand) Perm(n int) []int { 105 m := make([]int, n) 106 for i := 0; i < n; i++ { 107 m[i] = i 108 } 109 for i := 0; i < n; i++ { 110 j := r.Intn(i + 1) 111 m[i], m[j] = m[j], m[i] 112 } 113 return m 114 } 115 116 /* 117 * Top-level convenience functions 118 */ 119 120 var globalRand = New(&lockedSource{src: NewSource(1)}) 121 122 // Seed uses the provided seed value to initialize the default Source to a 123 // deterministic state. If Seed is not called, the generator behaves as 124 // if seeded by Seed(1). 125 func Seed(seed int64) { globalRand.Seed(seed) } 126 127 // Int63 returns a non-negative pseudo-random 63-bit integer as an int64 128 // from the default Source. 129 func Int63() int64 { return globalRand.Int63() } 130 131 // Uint32 returns a pseudo-random 32-bit value as a uint32 132 // from the default Source. 133 func Uint32() uint32 { return globalRand.Uint32() } 134 135 // Int31 returns a non-negative pseudo-random 31-bit integer as an int32 136 // from the default Source. 137 func Int31() int32 { return globalRand.Int31() } 138 139 // Int returns a non-negative pseudo-random int from the default Source. 140 func Int() int { return globalRand.Int() } 141 142 // Int63n returns, as an int64, a non-negative pseudo-random number in [0,n) 143 // from the default Source. 144 // It panics if n <= 0. 145 func Int63n(n int64) int64 { return globalRand.Int63n(n) } 146 147 // Int31n returns, as an int32, a non-negative pseudo-random number in [0,n) 148 // from the default Source. 149 // It panics if n <= 0. 150 func Int31n(n int32) int32 { return globalRand.Int31n(n) } 151 152 // Intn returns, as an int, a non-negative pseudo-random number in [0,n) 153 // from the default Source. 154 // It panics if n <= 0. 155 func Intn(n int) int { return globalRand.Intn(n) } 156 157 // Float64 returns, as a float64, a pseudo-random number in [0.0,1.0) 158 // from the default Source. 159 func Float64() float64 { return globalRand.Float64() } 160 161 // Float32 returns, as a float32, a pseudo-random number in [0.0,1.0) 162 // from the default Source. 163 func Float32() float32 { return globalRand.Float32() } 164 165 // Perm returns, as a slice of n ints, a pseudo-random permutation of the integers [0,n) 166 // from the default Source. 167 func Perm(n int) []int { return globalRand.Perm(n) } 168 169 // NormFloat64 returns a normally distributed float64 in the range 170 // [-math.MaxFloat64, +math.MaxFloat64] with 171 // standard normal distribution (mean = 0, stddev = 1) 172 // from the default Source. 173 // To produce a different normal distribution, callers can 174 // adjust the output using: 175 // 176 // sample = NormFloat64() * desiredStdDev + desiredMean 177 // 178 func NormFloat64() float64 { return globalRand.NormFloat64() } 179 180 // ExpFloat64 returns an exponentially distributed float64 in the range 181 // (0, +math.MaxFloat64] with an exponential distribution whose rate parameter 182 // (lambda) is 1 and whose mean is 1/lambda (1) from the default Source. 183 // To produce a distribution with a different rate parameter, 184 // callers can adjust the output using: 185 // 186 // sample = ExpFloat64() / desiredRateParameter 187 // 188 func ExpFloat64() float64 { return globalRand.ExpFloat64() } 189 190 type lockedSource struct { 191 lk sync.Mutex 192 src Source 193 } 194 195 func (r *lockedSource) Int63() (n int64) { 196 r.lk.Lock() 197 n = r.src.Int63() 198 r.lk.Unlock() 199 return 200 } 201 202 func (r *lockedSource) Seed(seed int64) { 203 r.lk.Lock() 204 r.src.Seed(seed) 205 r.lk.Unlock() 206 }