github.com/primecitizens/pcz/std@v0.2.1/algo/rand/rand.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright 2023 The Prime Citizens
     3  
     4  package rand
     5  
     6  type Core interface {
     7  	// Rand32 returns a random uint32.
     8  	Rand32() uint32
     9  
    10  	// Rand64 returns a random uint64.
    11  	Rand64() uint64
    12  }
    13  
    14  // Rand32n
    15  //
    16  // adapted from golang runtime.fastrandn
    17  // See https://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/
    18  func Rand32n[C Core](core C, n uint32) uint32 {
    19  	return uint32(uint64(core.Rand32()) * uint64(n) >> 32)
    20  }
    21  
    22  // Rand64n
    23  func Rand64n[C Core](core C, n uint64) uint64 {
    24  	return core.Rand64() % n
    25  }