src

Go monorepo.
git clone git://code.dwrz.net/src
Log | Files | Refs

rand.go (690B)


      1 package rand
      2 
      3 import (
      4 	"crypto/rand"
      5 	"fmt"
      6 	"io"
      7 	"math/big"
      8 )
      9 
     10 func init() {
     11 	Reader = rand.Reader
     12 }
     13 
     14 // Reader provides a random reader that can reset during testing.
     15 var Reader io.Reader
     16 
     17 // Int63n returns a int64 between zero and value of max, read from an io.Reader source.
     18 func Int63n(reader io.Reader, max int64) (int64, error) {
     19 	bi, err := rand.Int(reader, big.NewInt(max))
     20 	if err != nil {
     21 		return 0, fmt.Errorf("failed to read random value, %w", err)
     22 	}
     23 
     24 	return bi.Int64(), nil
     25 }
     26 
     27 // CryptoRandInt63n returns a random int64 between zero and value of max
     28 // obtained from the crypto rand source.
     29 func CryptoRandInt63n(max int64) (int64, error) {
     30 	return Int63n(Reader, max)
     31 }