Skip to content

Commit

Permalink
Upgrades
Browse files Browse the repository at this point in the history
  • Loading branch information
bogcon committed Nov 6, 2023
1 parent b7ab9c3 commit 96ecd75
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
timeout-minutes: 5
strategy:
matrix:
go-version: [1.16.x, 1.17.x, 1.18.x, 1.19.x, 1.20.x]
go-version: [1.20.x, 1.21.x]
platform: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.platform }}

Expand All @@ -30,9 +30,9 @@ jobs:
uses: actions/cache@v3
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
key: ${{ runner.os }}-go-${{ matrix.go-version }}-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
${{ runner.os }}-go-${{ matrix.go-version }}-
- name: Download dependencies
run: make setup
Expand All @@ -44,7 +44,7 @@ jobs:
run: make clean cover

- name: Upload coverage to coveralls.io
if: matrix.platform == 'ubuntu-latest' && matrix.go-version == '1.20.x'
if: matrix.platform == 'ubuntu-latest' && matrix.go-version == '1.21.x'
uses: shogo82148/actions-goveralls@v1
with:
path-to-profile: cover.out
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
LINTER_VERSION=v1.53.3
LINTER_VERSION=v1.55.2
LINTER=./bin/golangci-lint
ifeq ($(OS),Windows_NT)
LINTER=./bin/golangci-lint.exe
Expand Down
Empty file added go.sum
Empty file.
43 changes: 36 additions & 7 deletions rand.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,46 @@ import (
cRand "crypto/rand"
"encoding/binary"
mRand "math/rand"
"sync"
"time"
"unsafe"
)

// defaultJitterFactor is the factor to apply by default on the jitter.
const defaultJitterFactor = 0.2

// globalRand is a global instance of Rand.
var globalRand *mRand.Rand

// init initializes math rand with a secure random seed.
// Is called automatically by go, only once, on this package first import elsewhere.
func init() {
mRand.Seed(getRandSeed())
globalRand = mRand.New(&lockedSource{src: mRand.NewSource(getRandSeed())})
}

// lockedSource allows a random number generator to be used by multiple goroutines
// concurrently. The code is very similar to math/rand.lockedSource, which is
// unfortunately not exposed.
type lockedSource struct {
sync.Mutex

src mRand.Source
}

// Int63...
func (ls *lockedSource) Int63() (n int64) {
ls.Lock()
n = ls.src.Int63()
ls.Unlock()

return
}

// Seed...
func (ls *lockedSource) Seed(seed int64) {
ls.Lock()
ls.src.Seed(seed)
ls.Unlock()
}

// getRandSeed returns a random seed number.
Expand All @@ -43,18 +72,18 @@ func getRandSeed() int64 {
// Intn generates a random integer in range [0,n).
// It panics if max <= 0.
func Intn(n int) int {
return mRand.Intn(n)
return globalRand.Intn(n)
}

// IntnBetween generates a random integer in range [min,max).
// It panics if max <= 0.
func IntnBetween(min, max int) int {
return mRand.Intn(max-min) + min
return globalRand.Intn(max-min) + min
}

// Float64 generates a random float64 in range [0.0, 1.0).
func Float64() float64 {
return mRand.Float64()
return globalRand.Float64()
}

// Jitter returns a time.Duration altered with a random factor.
Expand All @@ -69,7 +98,7 @@ func Jitter(duration time.Duration, maxFactor ...float64) time.Duration {

newDuration := time.Duration(0)
for newDuration <= 0 {
randRange := 2*mRand.Float64() - 1 // [-1.0, 1.0)
randRange := 2*Float64() - 1 // [-1.0, 1.0)
jitter := time.Duration(randRange * factor * float64(duration))
newDuration = duration + jitter
}
Expand Down Expand Up @@ -103,11 +132,11 @@ func String(n int, alphabet ...string) string {
b = make([]byte, n)
)

randomInt63 := mRand.Int63()
randomInt63 := globalRand.Int63()
remaining := alphabetIdxMax
for i := 0; i < n; {
if remaining == 0 { // generate a new random 63 bits integer, reset remaining
randomInt63, remaining = mRand.Int63(), alphabetIdxMax
randomInt63, remaining = globalRand.Int63(), alphabetIdxMax
}
if alphabetIdx := int(randomInt63 & alphabetIdxMask); alphabetIdx < len(a) {
b[i] = a[alphabetIdx]
Expand Down

0 comments on commit 96ecd75

Please sign in to comment.