为什么当第一个累积值>=随机值时,轮盘选择中的循环会停止

本文关键字:选择 循环 第一个 为什么 随机 | 更新日期: 2023-09-27 18:32:47

在本文中,测试运行K-Means++,他使用C#代码和轮盘选择来获得下一个质心

有一个实现轮盘选择的代码

while (sanity < data.Length * 2)
{
    cumulative += dSquared[ii] / sum;
    if (cumulative >= p && used.Contains(ii) == false)
    {
        newMean = ii; // the chosen index
        used.Add(newMean); // don't pick again
        break; // WHY BREAK ?? THERE IS ANOTHER BIGGER CUMULATIVE VALUES
    }
    ++ii; // next candidate
    if (ii >= dSquared.Length) 
        ii = 0; // past the end
    ++sanity;
}  

但是当这里遇到第一个真实条件时为什么要打破:

if (cumulative >= p && used.Contains(ii) == false)

为什么不循环到索引 19 ???

N : 20 项目

随机值 = 0,817325359590969

我将代码的结果与Excel进行比较:结果(如果不停在索引16处)

谁能向我解释这个问题的答案?

为什么当第一个累积值>=随机值时,轮盘选择中的循环会停止

因为你想要一个加权随机抽样。

否则,您将始终选择最后一个值。