v8.run/go/exp@v0.0.26-0.20230226010534-afcdbd3f782d/fastrand/u32.go (about)

     1  package fastrand
     2  
     3  import (
     4  	_ "runtime"
     5  	_ "unsafe"
     6  
     7  	"v8.run/go/exp/util/mathutil"
     8  )
     9  
    10  func runtime_fastrand() uint32
    11  func runtime_fastrandn(n uint32) uint32
    12  
    13  //go:linkname runtime_fastrand runtime.fastrand
    14  //go:linkname runtime_fastrandn runtime.fastrandn
    15  
    16  func Uint32() uint32 {
    17  	return runtime_fastrand()
    18  }
    19  
    20  func Uint32n(n uint32) uint32 {
    21  	return runtime_fastrandn(n)
    22  }
    23  
    24  func Int32() int32 {
    25  	return int32(Uint32())
    26  }
    27  
    28  func Int31() int32 {
    29  	return int32(Uint32() & (1<<31 - 1))
    30  }
    31  
    32  func Int31n(n int32) int32 {
    33  	return int32(Uint32n(uint32(n)))
    34  }
    35  
    36  func (rng *RNG) Uint32() uint32 {
    37  	return uint32(rng.Uint64())
    38  }
    39  
    40  func (rng *RNG) Uint32n(n uint32) uint32 {
    41  	return mathutil.BoundUint32(uint32(rng.Uint64()), n)
    42  }
    43  
    44  func (rng *RNG) Int32() int32 {
    45  	return int32(rng.Uint32())
    46  }
    47  
    48  func (rng *RNG) Int31() int32 {
    49  	return int32(rng.Uint32() & (1<<31 - 1))
    50  }
    51  
    52  func (rng *RNG) Int31n(n int32) int32 {
    53  	return int32(rng.Uint32n(uint32(n)))
    54  }