Skip to content

Commit 85242ca

Browse files
committed
clarification about downcasting
1 parent 597c9a9 commit 85242ca

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

utils/rand/rand.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ func Uint32() (uint32, error) {
9090
// for 64-bits machines, doing 64 bits operations and then casting
9191
// should be faster than dealing with 32 bits operations
9292
r, err := Uint64()
93+
// 64 bits are sampled but only 32 bits are used. This does not affect the uniformity of the output
94+
// assuming that the 64-bits distribution is uniform
9395
return uint32(r), err
9496
}
9597

@@ -102,7 +104,7 @@ func Uint32() (uint32, error) {
102104
// - (random, nil) otherwise
103105
func Uint32n(n uint32) (uint32, error) {
104106
r, err := Uint64n(uint64(n))
105-
return uint32(r), err
107+
return uint32(r), err // `r` is less than `n` and necessarily fits in 32 bits
106108
}
107109

108110
// Uint returns a random uint.

0 commit comments

Comments
 (0)