github.com/searKing/golang/go@v1.2.117/math/rand/rand.go (about)

     1  // Copyright 2021 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package rand implements math/rand functions in a concurrent-safe way
     6  // with a global random source, independent of math/rand's global source.
     7  package rand
     8  
     9  import (
    10  	"math/rand"
    11  	"sync"
    12  	"time"
    13  )
    14  
    15  var (
    16  	globalRand = rand.New(rand.NewSource(time.Now().UnixNano()))
    17  	mu         sync.Mutex
    18  )
    19  
    20  // Seed implements rand.Seed on the global source.
    21  func Seed(seed int64) {
    22  	mu.Lock()
    23  	defer mu.Unlock()
    24  	rand.Int31()
    25  	globalRand.Seed(seed)
    26  }
    27  
    28  // Int63 implements rand.Int63 on the global source.
    29  func Int63() int64 {
    30  	mu.Lock()
    31  	defer mu.Unlock()
    32  	return globalRand.Int63()
    33  }
    34  
    35  // Uint32 implements rand.Uint32 on the global source.
    36  func Uint32() uint32 {
    37  	mu.Lock()
    38  	defer mu.Unlock()
    39  	return globalRand.Uint32()
    40  }
    41  
    42  // Uint64 implements rand.Uint64 on the global source.
    43  func Uint64() uint64 {
    44  	mu.Lock()
    45  	defer mu.Unlock()
    46  	return globalRand.Uint64()
    47  }
    48  
    49  // Int31 implements rand.Int31 on the global source.
    50  func Int31() int32 {
    51  	mu.Lock()
    52  	defer mu.Unlock()
    53  	return globalRand.Int31()
    54  }
    55  
    56  // Int implements rand.Int on the global source.
    57  func Int() int {
    58  	mu.Lock()
    59  	defer mu.Unlock()
    60  	return globalRand.Int()
    61  }
    62  
    63  // Int63n implements rand.Int63n on the global source.
    64  func Int63n(n int64) int64 {
    65  	mu.Lock()
    66  	defer mu.Unlock()
    67  	return globalRand.Int63n(n)
    68  }
    69  
    70  // Int31n implements rand.Int31n on the global source.
    71  func Int31n(n int32) int32 {
    72  	mu.Lock()
    73  	defer mu.Unlock()
    74  	return globalRand.Int31n(n)
    75  }
    76  
    77  // Intn implements rand.Intn on the global source.
    78  func Intn(n int) int {
    79  	mu.Lock()
    80  	defer mu.Unlock()
    81  	return globalRand.Intn(n)
    82  }
    83  
    84  // Float64 implements rand.Float64 on the global source.
    85  func Float64() float64 {
    86  	mu.Lock()
    87  	defer mu.Unlock()
    88  	return globalRand.Float64()
    89  }
    90  
    91  // Float32 implements rand.Float32 on the global source.
    92  func Float32() float32 {
    93  	mu.Lock()
    94  	defer mu.Unlock()
    95  	return globalRand.Float32()
    96  }
    97  
    98  // Perm implements rand.Perm on the global source.
    99  func Perm(n int) []int {
   100  	mu.Lock()
   101  	defer mu.Unlock()
   102  	return globalRand.Perm(n)
   103  }
   104  
   105  // Shuffle implements rand.Shuffle on the global source.
   106  func Shuffle(n int, swap func(i, j int)) {
   107  	mu.Lock()
   108  	defer mu.Unlock()
   109  	globalRand.Shuffle(n, swap)
   110  }
   111  
   112  // Read implements rand.Read on the global source.
   113  func Read(p []byte) (n int, err error) {
   114  	mu.Lock()
   115  	defer mu.Unlock()
   116  	return globalRand.Read(p)
   117  }