dubbo.apache.org/dubbo-go/v3@v3.1.1/xds/utils/grpcrand/grpcrand.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  /*
    19   *
    20   * Copyright 2018 gRPC authors.
    21   *
    22   */
    23  
    24  // Package grpcrand implements math/rand functions in a concurrent-safe way
    25  // with a global random source, independent of math/rand's global source.
    26  package grpcrand
    27  
    28  import (
    29  	"math/rand"
    30  	"sync"
    31  	"time"
    32  )
    33  
    34  var (
    35  	r  = rand.New(rand.NewSource(time.Now().UnixNano()))
    36  	mu sync.Mutex
    37  )
    38  
    39  // Int implements rand.Int on the grpcrand global source.
    40  func Int() int {
    41  	mu.Lock()
    42  	defer mu.Unlock()
    43  	return r.Int()
    44  }
    45  
    46  // Int63n implements rand.Int63n on the grpcrand global source.
    47  func Int63n(n int64) int64 {
    48  	mu.Lock()
    49  	defer mu.Unlock()
    50  	return r.Int63n(n)
    51  }
    52  
    53  // Intn implements rand.Intn on the grpcrand global source.
    54  func Intn(n int) int {
    55  	mu.Lock()
    56  	defer mu.Unlock()
    57  	return r.Intn(n)
    58  }
    59  
    60  // Float64 implements rand.Float64 on the grpcrand global source.
    61  func Float64() float64 {
    62  	mu.Lock()
    63  	defer mu.Unlock()
    64  	return r.Float64()
    65  }
    66  
    67  // Uint64 implements rand.Uint64 on the grpcrand global source.
    68  func Uint64() uint64 {
    69  	mu.Lock()
    70  	defer mu.Unlock()
    71  	return r.Uint64()
    72  }