Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions slice/skip/skip.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ package skip

import (
"math/rand"
"sync"
"time"
)

Expand All @@ -76,8 +77,13 @@ const p = .5 // the p level defines the probability that a node
// randomly seeded generator.
var generator = rand.New(rand.NewSource(time.Now().UnixNano()))

// rnLock protects the RNG as the generator is not threadsafe.
var rnLock sync.Mutex

func generateLevel(maxLevel uint8) uint8 {
var level uint8
rnLock.Lock()
defer rnLock.Unlock()
for level = uint8(1); level < maxLevel-1; level++ {
if generator.Float64() >= p {
return level
Expand Down