github.com/lulzWill/go-agent@v2.1.2+incompatible/internal/rand.go (about)

     1  package internal
     2  
     3  import (
     4  	"math/rand"
     5  	"sync"
     6  	"time"
     7  )
     8  
     9  var (
    10  	seededRand = struct {
    11  		sync.Mutex
    12  		*rand.Rand
    13  	}{
    14  		Rand: rand.New(rand.NewSource(int64(time.Now().UnixNano()))),
    15  	}
    16  )
    17  
    18  // RandUint64 returns a random uint64.
    19  //
    20  // IMPORTANT! The default rand package functions are not used, since we want to
    21  // minimize the chance that different Go processes duplicate the same
    22  // transaction id.  (Note that the rand top level functions "use a default
    23  // shared Source that produces a deterministic sequence of values each time a
    24  // program is run" (and we don't seed the shared Source to avoid changing
    25  // customer apps' behavior)).
    26  func RandUint64() uint64 {
    27  	seededRand.Lock()
    28  	defer seededRand.Unlock()
    29  
    30  	u1 := seededRand.Uint32()
    31  	u2 := seededRand.Uint32()
    32  	return (uint64(u1) << 32) | uint64(u2)
    33  }
    34  
    35  // RandUint32 returns a random uint32.
    36  func RandUint32() uint32 {
    37  	seededRand.Lock()
    38  	defer seededRand.Unlock()
    39  
    40  	return seededRand.Uint32()
    41  }