vendredi 30 octobre 2020

random generator correlates 100% - why?

I am trying to use Intel MKL random generator, but when checking it randomness through correlation, I can see I am getting 100% correlation - why?

Initialized MKL random generator:

    RandomGenerator::RandomGenerator()
{
    vslNewStream (&_stream, VSL_BRNG_MCG31, VSL_RNG_METHOD_UNIFORM_STD);

    vsRngUniform (VSL_RNG_METHOD_UNIFORM_STD, _stream, 1000, _numbers.data(), 0.0f, 1.0f);
    
        _index = 0;
    }

float
RandomGenerator::get_float(float a, float b)
{
    const auto u = _numbers[_index];

    _index++;

    _update();

    return a + (b - a) * u;
}

However, when checking the correlation of the different initializations, it correlates 100%. What could be the problem here?

    for (int32_t i = 0; i < 5; i++)
    {

        RandomGenerator random;

        int32_t ncount = 0;

        float avg = 0;

        std::vector<float> x, y;

        for (int32_t j = 0; j < 100; j++)
        {
            x.push_back(random.get_float(0,10));
        }

        for (int32_t j = 0; j < 100; j++)
        {
            y.push_back(random.get_float(0,10));
        }

        float avgx = 0;

        float avgy = 0;

        for (int32_t j = 0; j < 100; j++)
        {
            avgx = avgx + x[j];

            avgy = avgy + y[j];
        }

        avgx = avgx / 100;

        avgy = avgy / 100;

        float xx = 0;

        float yy = 0;

        for (int32_t j = 0; j < 100; j++)
        {
            xx = xx + (x[j] - avgx)*(y[j] - avgy);

            yy = yy + (x[j] - avgx)*(x[j] - avgx)*(y[j] - avgy)*(y[j] - avgy);
        }

        std::cout<<avgx<<std::endl<<avgy<<std::endl;

        std::cout<<xx/std::sqrt(yy)<<std::endl;

    }
}

Output:

4.99885 5.10161 0.120765 4.99885 5.10161 0.120765




Aucun commentaire:

Enregistrer un commentaire