From 433ba08c754dcca3d5b4e69e757b8de411207284 Mon Sep 17 00:00:00 2001 From: kercylan98 Date: Mon, 9 Oct 2023 14:16:46 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20random=20=E5=8C=85=E6=8C=89=E6=9D=83?= =?UTF-8?q?=E9=87=8D=E4=BA=A7=E7=94=9F=E7=BB=93=E6=9E=9C=E6=9B=B4=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=20int64?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/random/weight.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/utils/random/weight.go b/utils/random/weight.go index e931c4d..eaac060 100644 --- a/utils/random/weight.go +++ b/utils/random/weight.go @@ -1,20 +1,20 @@ package random // WeightSlice 按权重随机从切片中产生一个数据并返回 -func WeightSlice[T any](getWeightHandle func(data T) int, data ...T) T { +func WeightSlice[T any](getWeightHandle func(data T) int64, data ...T) T { item, _ := WeightSliceIndex(getWeightHandle, data...) return item } // WeightSliceIndex 按权重随机从切片中产生一个数据并返回数据和对应索引 -func WeightSliceIndex[T any](getWeightHandle func(data T) int, data ...T) (item T, index int) { - var total int - var overlayWeight []int +func WeightSliceIndex[T any](getWeightHandle func(data T) int64, data ...T) (item T, index int) { + var total int64 + var overlayWeight []int64 for _, d := range data { total += getWeightHandle(d) overlayWeight = append(overlayWeight, total) } - var r = IntN(total) + var r = Int64(0, total) var i, count = 0, len(overlayWeight) for i < count { h := int(uint(i+count) >> 1) @@ -28,15 +28,15 @@ func WeightSliceIndex[T any](getWeightHandle func(data T) int, data ...T) (item } // WeightMap 按权重随机从map中产生一个数据并返回 -func WeightMap[K comparable, T any](getWeightHandle func(data T) int, data map[K]T) T { +func WeightMap[K comparable, T any](getWeightHandle func(data T) int64, data map[K]T) T { item, _ := WeightMapKey(getWeightHandle, data) return item } // WeightMapKey 按权重随机从map中产生一个数据并返回数据和对应 key -func WeightMapKey[K comparable, T any](getWeightHandle func(data T) int, data map[K]T) (item T, key K) { - var total int - var overlayWeight []int +func WeightMapKey[K comparable, T any](getWeightHandle func(data T) int64, data map[K]T) (item T, key K) { + var total int64 + var overlayWeight []int64 var dataSlice = make([]T, 0, len(data)) var dataKeySlice = make([]K, 0, len(data)) for k, d := range data { @@ -45,7 +45,7 @@ func WeightMapKey[K comparable, T any](getWeightHandle func(data T) int, data ma dataKeySlice = append(dataKeySlice, k) overlayWeight = append(overlayWeight, total) } - var r = IntN(total) + var r = Int64(0, total) var i, count = 0, len(overlayWeight) for i < count { h := int(uint(i+count) >> 1)