@@ -21,22 +21,11 @@ import (
21
21
_ "unsafe" // for go:linkname
22
22
)
23
23
24
- // A Source represents a source of uniformly-distributed
25
- // pseudo-random int64 values in the range [0, 1<<63 ).
24
+ // A Source is a source of uniformly-distributed
25
+ // pseudo-random uint64 values in the range [0, 1<<64 ).
26
26
//
27
27
// A Source is not safe for concurrent use by multiple goroutines.
28
28
type Source interface {
29
- Int64 () int64
30
- }
31
-
32
- // A Source64 is a Source that can also generate
33
- // uniformly-distributed pseudo-random uint64 values in
34
- // the range [0, 1<<64) directly.
35
- // If a Rand r's underlying Source s implements Source64,
36
- // then r.Uint64 returns the result of one call to s.Uint64
37
- // instead of making two calls to s.Int64.
38
- type Source64 interface {
39
- Source
40
29
Uint64 () uint64
41
30
}
42
31
@@ -57,28 +46,23 @@ func newSource(seed int64) *rngSource {
57
46
// A Rand is a source of random numbers.
58
47
type Rand struct {
59
48
src Source
60
- s64 Source64 // non-nil if src is source64
61
49
}
62
50
63
51
// New returns a new Rand that uses random values from src
64
52
// to generate other random values.
65
53
func New (src Source ) * Rand {
66
- s64 , _ := src .(Source64 )
67
- return & Rand {src : src , s64 : s64 }
54
+ return & Rand {src : src }
68
55
}
69
56
70
57
// Int64 returns a non-negative pseudo-random 63-bit integer as an int64.
71
- func (r * Rand ) Int64 () int64 { return r .src .Int64 ( ) }
58
+ func (r * Rand ) Int64 () int64 { return int64 ( r .src .Uint64 () &^ ( 1 << 63 ) ) }
72
59
73
60
// Uint32 returns a pseudo-random 32-bit value as a uint32.
74
61
func (r * Rand ) Uint32 () uint32 { return uint32 (r .Int64 () >> 31 ) }
75
62
76
63
// Uint64 returns a pseudo-random 64-bit value as a uint64.
77
64
func (r * Rand ) Uint64 () uint64 {
78
- if r .s64 != nil {
79
- return r .s64 .Uint64 ()
80
- }
81
- return uint64 (r .Int64 ())>> 31 | uint64 (r .Int64 ())<< 32
65
+ return r .src .Uint64 ()
82
66
}
83
67
84
68
// Int32 returns a non-negative pseudo-random 31-bit integer as an int32.
0 commit comments