mirror of
https://github.com/vastxie/99AI.git
synced 2025-11-12 19:53:42 +08:00
NineAI 2.4.2
This commit is contained in:
17
dist/common/utils/selectKeyWithWeight.js
vendored
Normal file
17
dist/common/utils/selectKeyWithWeight.js
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.selectKeyWithWeight = void 0;
|
||||
function selectKeyWithWeight(data) {
|
||||
if (data.length === 0)
|
||||
return undefined;
|
||||
const totalWeight = data.reduce((sum, item) => sum + item.weight, 0);
|
||||
let randomWeight = Math.random() * totalWeight;
|
||||
for (const item of data) {
|
||||
randomWeight -= item.weight;
|
||||
if (randomWeight < 0) {
|
||||
return item;
|
||||
}
|
||||
}
|
||||
return data[data.length - 1];
|
||||
}
|
||||
exports.selectKeyWithWeight = selectKeyWithWeight;
|
||||
Reference in New Issue
Block a user