github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/utils/randat/rand_at.go (about)

     1  package randat
     2  
     3  import (
     4  	"math/rand"
     5  )
     6  
     7  type cached struct {
     8  	seed uint64
     9  	r    uint64
    10  }
    11  
    12  var (
    13  	gSeed = rand.Int63()
    14  	cache = cached{}
    15  )
    16  
    17  // RandAt returns random number with seed
    18  // Not safe for concurrent use
    19  func RandAt(seed uint64) uint64 {
    20  	if seed != 0 && cache.seed == seed {
    21  		return cache.r
    22  	}
    23  	cache.seed = seed
    24  	cache.r = rand.New(rand.NewSource(gSeed ^ int64(seed))).Uint64()
    25  	return cache.r
    26  }