Commit 3f69822a authored by Russ Cox's avatar Russ Cox

math/rand: export Source64, mainly for documentation value

There is some code value too: types intending to implement
Source64 can write a conversion confirming that.

For #4254 and the Go 1.8 release notes.

Change-Id: I7fc350a84f3a963e4dab317ad228fa340dda5c66
Reviewed-on: https://go-review.googlesource.com/33456
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 2f0a306d
...@@ -23,10 +23,13 @@ type Source interface { ...@@ -23,10 +23,13 @@ type Source interface {
Seed(seed int64) Seed(seed int64)
} }
// A source64 represents a Source, which is also a source // A Source64 is a Source that can also generate
// of uniformly-distributed pseudo-random uint64 values in // uniformly-distributed pseudo-random uint64 values in
// the range [0, 1<<64). // the range [0, 1<<64) directly.
type source64 interface { // If a Rand r's underlying Source s implements Source64,
// then r.Uint64 returns the result of one call to s.Uint64
// instead of making two calls to s.Int63.
type Source64 interface {
Source Source
Uint64() uint64 Uint64() uint64
} }
...@@ -43,7 +46,7 @@ func NewSource(seed int64) Source { ...@@ -43,7 +46,7 @@ func NewSource(seed int64) Source {
// A Rand is a source of random numbers. // A Rand is a source of random numbers.
type Rand struct { type Rand struct {
src Source src Source
s64 source64 // non-nil if src is source64 s64 Source64 // non-nil if src is source64
// readVal contains remainder of 63-bit integer used for bytes // readVal contains remainder of 63-bit integer used for bytes
// generation during most recent Read call. // generation during most recent Read call.
...@@ -58,7 +61,7 @@ type Rand struct { ...@@ -58,7 +61,7 @@ type Rand struct {
// New returns a new Rand that uses random values from src // New returns a new Rand that uses random values from src
// to generate other random values. // to generate other random values.
func New(src Source) *Rand { func New(src Source) *Rand {
s64, _ := src.(source64) s64, _ := src.(Source64)
return &Rand{src: src, s64: s64} return &Rand{src: src, s64: s64}
} }
...@@ -229,7 +232,7 @@ func read(p []byte, int63 func() int64, readVal *int64, readPos *int8) (n int, e ...@@ -229,7 +232,7 @@ func read(p []byte, int63 func() int64, readVal *int64, readPos *int8) (n int, e
* Top-level convenience functions * Top-level convenience functions
*/ */
var globalRand = New(&lockedSource{src: NewSource(1).(source64)}) var globalRand = New(&lockedSource{src: NewSource(1).(Source64)})
// Seed uses the provided seed value to initialize the default Source to a // Seed uses the provided seed value to initialize the default Source to a
// deterministic state. If Seed is not called, the generator behaves as // deterministic state. If Seed is not called, the generator behaves as
...@@ -312,7 +315,7 @@ func ExpFloat64() float64 { return globalRand.ExpFloat64() } ...@@ -312,7 +315,7 @@ func ExpFloat64() float64 { return globalRand.ExpFloat64() }
type lockedSource struct { type lockedSource struct {
lk sync.Mutex lk sync.Mutex
src source64 src Source64
} }
func (r *lockedSource) Int63() (n int64) { func (r *lockedSource) Int63() (n int64) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment