github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/amino/libs/detrand/detrand.go (about)

     1  package detrand
     2  
     3  import "math/rand"
     4  
     5  // XXX copied from cosmos-sdk simulation.  Make a library?
     6  // DeriveRand derives a new Rand deterministically from another random source.
     7  // Unlike rand.New(rand.NewSource(seed)), the result is "more random"
     8  // depending on the source and state of r.
     9  //
    10  // NOTE: not crypto safe.
    11  func DeriveRand(r *rand.Rand) *rand.Rand {
    12  	const num = 8 // TODO what's a good number?  Too large is too slow.
    13  	ms := multiSource(make([]rand.Source, num))
    14  
    15  	for i := 0; i < num; i++ {
    16  		ms[i] = rand.NewSource(r.Int63())
    17  	}
    18  
    19  	//nolint:gosec
    20  	return rand.New(ms)
    21  }
    22  
    23  type multiSource []rand.Source
    24  
    25  func (ms multiSource) Int63() (r int64) {
    26  	for _, source := range ms {
    27  		r ^= source.Int63()
    28  	}
    29  
    30  	return r
    31  }
    32  
    33  func (ms multiSource) Seed(seed int64) {
    34  	panic("multiSource Seed should not be called")
    35  }