github.com/sandwich-go/boost@v1.3.29/z/rand.go (about)

     1  package z
     2  
     3  import (
     4  	_ "unsafe"
     5  )
     6  
     7  // FastRand is a fast thread local random function.
     8  //go:linkname FastRand runtime.fastrand
     9  func FastRand() uint32
    10  
    11  // FastRandUint32n returns pseudorandom uint32 in the range [0..maxN).
    12  // It is safe calling this function from concurrent goroutines.
    13  func FastRandUint32n(maxN uint32) uint32 {
    14  	x := FastRand()
    15  	// See http://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/
    16  	return uint32((uint64(x) * uint64(maxN)) >> 32)
    17  }