OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // Package rand implements pseudo-random number generators. | 5 // Package rand implements pseudo-random number generators. |
6 // | 6 // |
7 // Random numbers are generated by a Source. Top-level functions, such as | 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 | 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 | 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. | 10 // initialize the default Source if different behavior is required for each run. |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 if n <= 0 { | 88 if n <= 0 { |
89 panic("invalid argument to Intn") | 89 panic("invalid argument to Intn") |
90 } | 90 } |
91 if n <= 1<<31-1 { | 91 if n <= 1<<31-1 { |
92 return int(r.Int31n(int32(n))) | 92 return int(r.Int31n(int32(n))) |
93 } | 93 } |
94 return int(r.Int63n(int64(n))) | 94 return int(r.Int63n(int64(n))) |
95 } | 95 } |
96 | 96 |
97 // Float64 returns, as a float64, a pseudo-random number in [0.0,1.0). | 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) } | 98 func (r *Rand) Float64() float64 { return float64(r.Int63n(1<<53)) / (1 << 53) } |
99 | 99 |
100 // Float32 returns, as a float32, a pseudo-random number in [0.0,1.0). | 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()) } | 101 func (r *Rand) Float32() float32 { return float32(r.Int31n(1<<24)) / (1 << 24) } |
102 | 102 |
103 // Perm returns, as a slice of n ints, a pseudo-random permutation of the intege
rs [0,n). | 103 // Perm returns, as a slice of n ints, a pseudo-random permutation of the intege
rs [0,n). |
104 func (r *Rand) Perm(n int) []int { | 104 func (r *Rand) Perm(n int) []int { |
105 m := make([]int, n) | 105 m := make([]int, n) |
106 for i := 0; i < n; i++ { | 106 for i := 0; i < n; i++ { |
107 m[i] = i | 107 m[i] = i |
108 } | 108 } |
109 for i := 0; i < n; i++ { | 109 for i := 0; i < n; i++ { |
110 j := r.Intn(i + 1) | 110 j := r.Intn(i + 1) |
111 m[i], m[j] = m[j], m[i] | 111 m[i], m[j] = m[j], m[i] |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 n = r.src.Int63() | 197 n = r.src.Int63() |
198 r.lk.Unlock() | 198 r.lk.Unlock() |
199 return | 199 return |
200 } | 200 } |
201 | 201 |
202 func (r *lockedSource) Seed(seed int64) { | 202 func (r *lockedSource) Seed(seed int64) { |
203 r.lk.Lock() | 203 r.lk.Lock() |
204 r.src.Seed(seed) | 204 r.src.Seed(seed) |
205 r.lk.Unlock() | 205 r.lk.Unlock() |
206 } | 206 } |
OLD | NEW |