grpcrand_go1.21.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //go:build go1.21
  2. /*
  3. *
  4. * Copyright 2024 gRPC authors.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. */
  19. // Package grpcrand implements math/rand functions in a concurrent-safe way
  20. // with a global random source, independent of math/rand's global source.
  21. package grpcrand
  22. import "math/rand"
  23. // This implementation will be used for Go version 1.21 or newer.
  24. // For older versions, the original implementation with mutex will be used.
  25. // Int implements rand.Int on the grpcrand global source.
  26. func Int() int {
  27. return rand.Int()
  28. }
  29. // Int63n implements rand.Int63n on the grpcrand global source.
  30. func Int63n(n int64) int64 {
  31. return rand.Int63n(n)
  32. }
  33. // Intn implements rand.Intn on the grpcrand global source.
  34. func Intn(n int) int {
  35. return rand.Intn(n)
  36. }
  37. // Int31n implements rand.Int31n on the grpcrand global source.
  38. func Int31n(n int32) int32 {
  39. return rand.Int31n(n)
  40. }
  41. // Float64 implements rand.Float64 on the grpcrand global source.
  42. func Float64() float64 {
  43. return rand.Float64()
  44. }
  45. // Uint64 implements rand.Uint64 on the grpcrand global source.
  46. func Uint64() uint64 {
  47. return rand.Uint64()
  48. }
  49. // Uint32 implements rand.Uint32 on the grpcrand global source.
  50. func Uint32() uint32 {
  51. return rand.Uint32()
  52. }
  53. // ExpFloat64 implements rand.ExpFloat64 on the grpcrand global source.
  54. func ExpFloat64() float64 {
  55. return rand.ExpFloat64()
  56. }
  57. // Shuffle implements rand.Shuffle on the grpcrand global source.
  58. var Shuffle = func(n int, f func(int, int)) {
  59. rand.Shuffle(n, f)
  60. }