Repository: opencv/opencv
File: modules/imgproc/src/shapedescr.cpp (C++)

Code before:
    {
        dx = center.x - (float)pts[j].x;
        dy = center.y - (float)pts[j].y;
        if (norm(Point2f(dx, dy)) < radius)
        {
            continue;
        }
        else
        {
            findThirdPoint(pts, i, j, center, radius);
        }
    }
}


template<typename PT>
static void findMinEnclosingCircle(const PT *pts_in, int count, Point2f &center, float &radius)
{
    // Welzl's algorithm requires random permutation for expected O(n) time.
    // Without shuffling, sorted inputs trigger O(n^3) worst case.
    cv::AutoBuffer<PT, 1024> pts_buf(count);
    std::copy(pts_in, pts_in + count, pts_buf.data());
    PT* pts = pts_buf.data();
    if (count > 10)
    {
        cv::RNG rng(cv::theRNG().next());
        for (int i = count - 1; i > 0; --i)
        {
            int j = (unsigned int)rng % (i + 1);
            std::swap(pts[i], pts[j]);
        }
    }
    const PT *pts_ptr = pts;

    center.x = (float)(pts_ptr[0].x + pts_ptr[1].x) / 2.0f;
    center.y = (float)(pts_ptr[0].y + pts_ptr[1].y) / 2.0f;
    float dx = (float)(pts_ptr[0].x - pts_ptr[1].x);
    float dy = (float)(pts_ptr[0].y - pts_ptr[1].y);
    radius = (float)norm(Point2f(dx, dy)) / 2.0f + EPS;

    for (int i = 2; i < count; ++i)
    {
        dx = (float)pts_ptr[i].x - center.x;
        dy = (float)pts_ptr[i].y - center.y;
        float d = (float)norm(Point2f(dx, dy));
        if (d < radius)
        {
            continue;
        }
        else
        {

Review comment (suggestion):
do not initialize rng from theRNG. This will make function behavior non-deterministic (the subsequent runs of the same function on the same data must provide the same results). Instead, use some fixed seed, like 0x12345.

Code after:
    {
        dx = center.x - (float)pts[j].x;
        dy = center.y - (float)pts[j].y;
        if (norm(Point2f(dx, dy)) < radius)
        {
            continue;
        }
        else
        {
            findThirdPoint(pts, i, j, center, radius);
        }
    }
}


template<typename PT>
static void findMinEnclosingCircle(const PT *pts_in, int count, Point2f &center, float &radius)
{
    // Welzl's algorithm requires random permutation for expected O(n) time.
    // Without shuffling, sorted inputs trigger O(n^3) worst case.
    cv::AutoBuffer<PT, 1024> pts_buf(count);
    std::copy(pts_in, pts_in + count, pts_buf.data());
    PT* pts = pts_buf.data();
    if (count > 10)
    {
        Cv32suf x0, y0, xn, yn;
        x0.f = (float)pts[0].x;
        y0.f = (float)pts[0].y;
        xn.f = (float)pts[count-1].x;
        yn.f = (float)pts[count-1].y;

        uint32_t seed = (uint32_t)count ^ x0.u ^ (y0.u << 8) ^ (xn.u << 16) ^ (yn.u << 24);
        cv::RNG rng(seed);

        for (int i = 1; i < count; ++i)
        {
            int j = rng.uniform(0, i + 1);
            std::swap(pts[i], pts[j]);
        }
    }

    center.x = (float)(pts[0].x + pts[1].x) / 2.0f;
    center.y = (float)(pts[0].y + pts[1].y) / 2.0f;
    float dx = (float)(pts[0].x - pts[1].x);
    float dy = (float)(pts[0].y - pts[1].y);
    radius = (float)norm(Point2f(dx, dy)) / 2.0f + EPS;

    for (int i = 2; i < count; ++i)
    {
        dx = (float)pts[i].x - center.x;
        dy = (float)pts[i].y - center.y;