go.x2ox.com/sorbifolia/random@v0.0.0-20240520090142-6d8be5c4ed59/rand.go (about)

     1  package random
     2  
     3  import (
     4  	"math/rand"
     5  )
     6  
     7  func Pick[T any](items []T) T {
     8  	return items[Fast().Uintn(uint(len(items)))]
     9  }
    10  
    11  func Picks[T any](items []T, num int) []T {
    12  	if len(items) == 0 || num <= 0 {
    13  		return nil
    14  	}
    15  
    16  	arr := make([]T, num)
    17  	for i := 0; i < num; i++ {
    18  		arr[i] = Pick(items)
    19  	}
    20  	return arr
    21  }
    22  
    23  func Shuffle[T any](items []T) {
    24  	rand.Shuffle(len(items), func(i, j int) {
    25  		items[i], items[j] = items[j], items[i]
    26  	})
    27  }
    28  
    29  func Reverse[T any](collection []T) {
    30  	length := len(collection)
    31  
    32  	for i := 0; i < length/2; i = i + 1 {
    33  		j := length - 1 - i
    34  		collection[i], collection[j] = collection[j], collection[i]
    35  	}
    36  }