Skip to content

Commit

Permalink
调整:随机数
Browse files Browse the repository at this point in the history
  • Loading branch information
steden committed Jul 16, 2024
1 parent cd98466 commit 3e005bd
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions enumerable.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"reflect"
"strings"
"sync"
"time"
)

type Enumerable[T any] struct {
Expand Down Expand Up @@ -1001,20 +1002,19 @@ func (receiver Enumerable[T]) RangeStart(startIndex int) Enumerable[T] {
}

// Rand 返回随机元素
func (receiver Enumerable[T]) Rand() T {
func (receiver Enumerable[T]) Rand() *T {
if receiver.lock == nil {
var t T
return t
return nil
}

receiver.lock.RLock()
defer receiver.lock.RUnlock()

if receiver.Count() < 2 {
return receiver.First()
return &(*receiver.source)[0]
}
random := rand.Intn(receiver.Count())
return (*receiver.source)[random]
random := rand.New(rand.NewSource(time.Now().UnixNano())).Intn(receiver.Count())
return &(*receiver.source)[random]
}

// ToString 将集合转成字符串,并用split分隔
Expand Down

0 comments on commit 3e005bd

Please sign in to comment.