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.
5 // Package rand implements pseudo-random number generators.
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.
16 // A Source represents a source of uniformly-distributed
17 // pseudo-random int64 values in the range [0, 1<<63).
18 type Source interface {
23 // NewSource returns a new pseudo-random Source seeded with the given value.
24 func NewSource(seed int64) Source {
30 // A Rand is a source of random numbers.
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} }
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) }
42 // Int63 returns a non-negative pseudo-random 63-bit integer as an int64.
43 func (r *Rand) Int63() int64 { return r.src.Int63() }
45 // Uint32 returns a pseudo-random 32-bit value as a uint32.
46 func (r *Rand) Uint32() uint32 { return uint32(r.Int63() >> 31) }
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) }
51 // Int returns a non-negative pseudo-random int.
52 func (r *Rand) Int() int {
54 return int(u << 1 >> 1) // clear sign bit if int == int32
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 {
61 panic("invalid argument to Int63n")
63 max := int64((1 << 63) - 1 - (1<<63)%uint64(n))
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 {
75 panic("invalid argument to Int31n")
77 max := int32((1 << 31) - 1 - (1<<31)%uint32(n))
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 {
89 panic("invalid argument to Intn")
92 return int(r.Int31n(int32(n)))
94 return int(r.Int63n(int64(n)))
97 // Float64 returns, as a float64, a pseudo-random number in [0.0,1.0).
98 func (r *Rand) Float64() float64 { return float64(r.Int63n(1<<53)) / (1 << 53) }
100 // Float32 returns, as a float32, a pseudo-random number in [0.0,1.0).
101 func (r *Rand) Float32() float32 { return float32(r.Int31n(1<<24)) / (1 << 24) }
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 {
106 for i := 0; i < n; i++ {
115 * Top-level convenience functions
118 var globalRand = New(&lockedSource{src: NewSource(1)})
120 // Seed uses the provided seed value to initialize the default Source to a
121 // deterministic state. If Seed is not called, the generator behaves as
122 // if seeded by Seed(1).
123 func Seed(seed int64) { globalRand.Seed(seed) }
125 // Int63 returns a non-negative pseudo-random 63-bit integer as an int64
126 // from the default Source.
127 func Int63() int64 { return globalRand.Int63() }
129 // Uint32 returns a pseudo-random 32-bit value as a uint32
130 // from the default Source.
131 func Uint32() uint32 { return globalRand.Uint32() }
133 // Int31 returns a non-negative pseudo-random 31-bit integer as an int32
134 // from the default Source.
135 func Int31() int32 { return globalRand.Int31() }
137 // Int returns a non-negative pseudo-random int from the default Source.
138 func Int() int { return globalRand.Int() }
140 // Int63n returns, as an int64, a non-negative pseudo-random number in [0,n)
141 // from the default Source.
142 // It panics if n <= 0.
143 func Int63n(n int64) int64 { return globalRand.Int63n(n) }
145 // Int31n returns, as an int32, a non-negative pseudo-random number in [0,n)
146 // from the default Source.
147 // It panics if n <= 0.
148 func Int31n(n int32) int32 { return globalRand.Int31n(n) }
150 // Intn returns, as an int, a non-negative pseudo-random number in [0,n)
151 // from the default Source.
152 // It panics if n <= 0.
153 func Intn(n int) int { return globalRand.Intn(n) }
155 // Float64 returns, as a float64, a pseudo-random number in [0.0,1.0)
156 // from the default Source.
157 func Float64() float64 { return globalRand.Float64() }
159 // Float32 returns, as a float32, a pseudo-random number in [0.0,1.0)
160 // from the default Source.
161 func Float32() float32 { return globalRand.Float32() }
163 // Perm returns, as a slice of n ints, a pseudo-random permutation of the integers [0,n)
164 // from the default Source.
165 func Perm(n int) []int { return globalRand.Perm(n) }
167 // NormFloat64 returns a normally distributed float64 in the range
168 // [-math.MaxFloat64, +math.MaxFloat64] with
169 // standard normal distribution (mean = 0, stddev = 1)
170 // from the default Source.
171 // To produce a different normal distribution, callers can
172 // adjust the output using:
174 // sample = NormFloat64() * desiredStdDev + desiredMean
176 func NormFloat64() float64 { return globalRand.NormFloat64() }
178 // ExpFloat64 returns an exponentially distributed float64 in the range
179 // (0, +math.MaxFloat64] with an exponential distribution whose rate parameter
180 // (lambda) is 1 and whose mean is 1/lambda (1) from the default Source.
181 // To produce a distribution with a different rate parameter,
182 // callers can adjust the output using:
184 // sample = ExpFloat64() / desiredRateParameter
186 func ExpFloat64() float64 { return globalRand.ExpFloat64() }
188 type lockedSource struct {
193 func (r *lockedSource) Int63() (n int64) {
200 func (r *lockedSource) Seed(seed int64) {