Biased random generator

This commit is contained in:
Ian Gulliver
2021-11-20 15:20:42 -10:00
parent 51f318c924
commit c12a546447
2 changed files with 43 additions and 0 deletions

10
gen/rand.go Normal file
View File

@@ -0,0 +1,10 @@
package gen
import "math/rand"
// Generate a random uint64 with an even distribution of bits.Len64()
func RandBiasedUint64() uint64 {
// The shift-right by up to 64 (shifting it to 0) makes up for randomness
// lost by setting the high bit.
return (rand.Uint64() | 0x8000000000000000) >> rand.Int31n(65)
}