github.com/kaydxh/golang@v0.0.131/go/math/rand/rand.go (about)

     1  /*
     2   *Copyright (c) 2022, kaydxh
     3   *
     4   *Permission is hereby granted, free of charge, to any person obtaining a copy
     5   *of this software and associated documentation files (the "Software"), to deal
     6   *in the Software without restriction, including without limitation the rights
     7   *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   *copies of the Software, and to permit persons to whom the Software is
     9   *furnished to do so, subject to the following conditions:
    10   *
    11   *The above copyright notice and this permission notice shall be included in all
    12   *copies or substantial portions of the Software.
    13   *
    14   *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   *SOFTWARE.
    21   */
    22  package rand
    23  
    24  import (
    25  	"fmt"
    26  	"math/rand"
    27  	"sync"
    28  	"time"
    29  )
    30  
    31  var (
    32  	globalRand  = rand.New(rand.NewSource(time.Now().UnixNano()))
    33  	mu          sync.Mutex
    34  	letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
    35  )
    36  
    37  // Int implements rand.Int on the global source.
    38  func Int() int {
    39  	mu.Lock()
    40  	defer mu.Unlock()
    41  	return globalRand.Int()
    42  }
    43  
    44  // Int31n implements rand.Int31n on the global source.
    45  func Int31n(n int32) int32 {
    46  	mu.Lock()
    47  	defer mu.Unlock()
    48  	return globalRand.Int31n(n)
    49  }
    50  
    51  // Uint32 implements rand.Uint32 on the global source.
    52  func Uint32() uint32 {
    53  	mu.Lock()
    54  	defer mu.Unlock()
    55  	return globalRand.Uint32()
    56  }
    57  
    58  // Int63n implements rand.Int63n on the global source.
    59  func Int63n(n int64) int64 {
    60  	mu.Lock()
    61  	defer mu.Unlock()
    62  	return globalRand.Int63n(n)
    63  }
    64  
    65  // Intn implements rand.Intn on the global source.
    66  func Intn(n int) int {
    67  	mu.Lock()
    68  	defer mu.Unlock()
    69  	return globalRand.Intn(n)
    70  }
    71  
    72  // Float32 implements rand.Float32 on the global source.
    73  func Float32() float32 {
    74  	mu.Lock()
    75  	defer mu.Unlock()
    76  	return globalRand.Float32()
    77  }
    78  
    79  // Float64 implements rand.Float64 on the global source.
    80  func Float64() float64 {
    81  	mu.Lock()
    82  	defer mu.Unlock()
    83  	return globalRand.Float64()
    84  }
    85  
    86  // Uint64 implements rand.Uint64 on the global source.
    87  func Uint64() uint64 {
    88  	mu.Lock()
    89  	defer mu.Unlock()
    90  	return globalRand.Uint64()
    91  }
    92  
    93  // Read implements rand.Read on the global source.
    94  func Read(p []byte) (n int, err error) {
    95  	mu.Lock()
    96  	defer mu.Unlock()
    97  	return globalRand.Read(p)
    98  }
    99  
   100  // RandInt generate number [min, max).
   101  func RangeInt(min, max int) (int, error) {
   102  	if min < 0 || max < 0 || max <= min {
   103  		return 0, fmt.Errorf("min or max must > 0 and max > min")
   104  	}
   105  	return Intn(max-min) + min, nil
   106  }
   107  
   108  //  RangeString generate string length [0, n].
   109  func RangeString(n int) string {
   110  	b := make([]rune, n)
   111  	for i := range b {
   112  		b[i] = letterRunes[rand.Intn(len(letterRunes))]
   113  	}
   114  	return string(b)
   115  }