Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
09a9f14a29 | ||
|
|
e0afb55bb2 |
@@ -53,6 +53,44 @@ func (m *randMod) RandomAll(data map[int]int, n int) []int {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func RandomAll[t Any](data map[t]int, n int) []t {
|
||||||
|
if n > len(data) {
|
||||||
|
n = len(data)
|
||||||
|
}
|
||||||
|
rand.Seed(time.Now().UnixNano())
|
||||||
|
// 复制权重映射,避免修改原始数据
|
||||||
|
remainingWeights := make(map[t]int)
|
||||||
|
for k, v := range data {
|
||||||
|
remainingWeights[k] = v
|
||||||
|
}
|
||||||
|
result := make([]t, 0, n)
|
||||||
|
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
totalWeight := 0
|
||||||
|
// 计算剩余元素的总权重
|
||||||
|
for _, weight := range remainingWeights {
|
||||||
|
totalWeight += weight
|
||||||
|
}
|
||||||
|
if totalWeight == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
// 生成一个 0 到总权重之间的随机数
|
||||||
|
randomNum := rand.Intn(totalWeight)
|
||||||
|
currentWeight := 0
|
||||||
|
for key, weight := range remainingWeights {
|
||||||
|
currentWeight += weight
|
||||||
|
if randomNum < currentWeight {
|
||||||
|
// 将选中的元素添加到结果切片中
|
||||||
|
result = append(result, key)
|
||||||
|
// 从剩余权重映射中移除选中的元素
|
||||||
|
delete(remainingWeights, key)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
// RandByArrInt 根据传入的 interface 切片中的整数值按权重随机返回一个索引
|
// RandByArrInt 根据传入的 interface 切片中的整数值按权重随机返回一个索引
|
||||||
// 参数 s: 一个包含整数的 interface 切片,切片中的每个元素代表一个权重
|
// 参数 s: 一个包含整数的 interface 切片,切片中的每个元素代表一个权重
|
||||||
// 返回值: 随机选中的元素的索引
|
// 返回值: 随机选中的元素的索引
|
||||||
|
|||||||
@@ -124,7 +124,7 @@ func (m *timeMod) GetDailyTimeList(time1 time.Time, time2 time.Time) (timeList [
|
|||||||
|
|
||||||
// ExcelTime2Time excel时间转时间 (12/10/24 02:03转为时间)
|
// ExcelTime2Time excel时间转时间 (12/10/24 02:03转为时间)
|
||||||
func (m *timeMod) ExcelTime2Time(excelTime string) time.Time {
|
func (m *timeMod) ExcelTime2Time(excelTime string) time.Time {
|
||||||
layout := "01/02/06 15:04" // 月/日/年(最后两位) 小时:分钟 (24小时制)
|
layout := "1/2/06 15:04" // 月/日/年(最后两位) 小时:分钟 (24小时制)
|
||||||
timeNew, _ := time.ParseInLocation(layout, excelTime, time.Local)
|
timeNew, _ := time.ParseInLocation(layout, excelTime, time.Local)
|
||||||
return timeNew
|
return timeNew
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,9 +20,9 @@ type Number interface {
|
|||||||
int | int64 | int32 | int16 | uint64 | uint32 | uint16 | float32 | float64
|
int | int64 | int32 | int16 | uint64 | uint32 | uint16 | float32 | float64
|
||||||
}
|
}
|
||||||
|
|
||||||
//type Any interface {
|
type Any interface {
|
||||||
// interface{} | string | int | int64 | int32 | int16 | uint64 | uint32 | uint16 | float32 | float64
|
string | int | int64 | int32 | int16 | uint64 | uint32 | uint16 | float32 | float64
|
||||||
//}
|
}
|
||||||
|
|
||||||
type toolsInterface interface {
|
type toolsInterface interface {
|
||||||
Load()
|
Load()
|
||||||
|
|||||||
Reference in New Issue
Block a user