From 064e94ee5c2d93b958bf687abf7cc271e776fd12 Mon Sep 17 00:00:00 2001 From: luoweijian <1329394916@qq.com> Date: Tue, 17 Mar 2026 16:37:52 +0800 Subject: [PATCH] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/AssemblePaperDomainServiceImpl.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/project/exam/domain/service/impl/AssemblePaperDomainServiceImpl.java b/src/main/java/com/project/exam/domain/service/impl/AssemblePaperDomainServiceImpl.java index 87cf11d..80c341b 100644 --- a/src/main/java/com/project/exam/domain/service/impl/AssemblePaperDomainServiceImpl.java +++ b/src/main/java/com/project/exam/domain/service/impl/AssemblePaperDomainServiceImpl.java @@ -206,7 +206,15 @@ public class AssemblePaperDomainServiceImpl implements AssemblePaperDomainServic }); // 带权乱序(A-Res算法) return kpList.stream() - .sorted(Comparator.comparingDouble(kp -> -Math.pow(ThreadLocalRandom.current().nextDouble(), 1.0 / kp.getWeightScore()))) + // 1. 先把对象和计算好的随机分数封装起来(保证每个元素在本次排序中分数固定) + .map(kp -> { + double score = Math.pow(ThreadLocalRandom.current().nextDouble(), 1.0 / kp.getWeightScore()); + return new AbstractMap.SimpleEntry<>(kp, score); + }) + // 2. 根据预先计算好的分数进行排序(降序排列,分数越高权重越大) + .sorted((e1, e2) -> Double.compare(e2.getValue(), e1.getValue())) + // 3. 还原回原始对象并取结果 + .map(Map.Entry::getKey) .limit(totalNum) .toList(); }