Repository: opencv/opencv
File: modules/dnn/src/layers/randomnormallike_layer.cpp (C++)

Code before:
        outputs.assign(1, inputs[0]);
        return false;
    }

    virtual void forward(InputArrayOfArrays inputs_arr,
                         OutputArrayOfArrays outputs_arr,
                         OutputArrayOfArrays internals_arr) CV_OVERRIDE
    {
        CV_UNUSED(internals_arr);
        CV_TRACE_FUNCTION();
        CV_TRACE_ARG_VALUE(name, "name", name.c_str());

        std::vector<Mat> inputs, outputs;
        inputs_arr.getMatVector(inputs);
        outputs_arr.getMatVector(outputs);

        CV_Assert(!inputs.empty());
        CV_Assert(!outputs.empty());

        Mat& out = outputs[0];

        RNG seededRng;
        RNG* rng = &theRNG();
        if (hasSeed)
        {
            seededRng = RNG(static_cast<uint64>(seed));
            rng = &seededRng;
        }

        if (out.depth() == CV_32F || out.depth() == CV_64F || out.depth() == CV_16F)
        {
            rng->fill(out, RNG::NORMAL, mean, scale);
        }
        else
        {
            Mat tmp(out.size.dims(), out.size.p, CV_32F);
            rng->fill(tmp, RNG::NORMAL, mean, scale);
            tmp.convertTo(out, out.type());
        }
    }

private:
    double mean;
    double scale;
    bool hasSeed = false;
    uint64_t seed = 0;
    int depth;
};

CV_DNN_REGISTER_LAYER_CLASS_STATIC(RandomNormalLike, RandomNormalLikeLayerImpl);


Review comment (suggestion):
I don't quite like that seed is rounded to unsigned integer before passing to RNG. That is, if I pass seed 1.1 and then 1.2, I will get the same results even though the seed has different values. Can we use the following approach instead?

```
Cvt64suf u;
u.f = seed;
seededRng = RNG(u.u);
rng = &seededRng;
```



Code after:
        CV_CheckEQ(inputs.size(), 1ull, "RandomNormalLike: one input is expected");

        outputs.assign(1, inputs[0]);
        return false;
    }

    virtual void forward(InputArrayOfArrays inputs_arr,
                         OutputArrayOfArrays outputs_arr,
                         OutputArrayOfArrays internals_arr) CV_OVERRIDE
    {
        CV_UNUSED(internals_arr);
        CV_TRACE_FUNCTION();
        CV_TRACE_ARG_VALUE(name, "name", name.c_str());

        std::vector<Mat> inputs, outputs;
        inputs_arr.getMatVector(inputs);
        outputs_arr.getMatVector(outputs);

        CV_Assert(!inputs.empty());
        CV_Assert(!outputs.empty());

        Mat out = outputs[0];
        const int desiredDepth = depth;
        const bool needRecreate = out.depth() != desiredDepth;
        Mat outBlob;
        if (needRecreate)
        {
            const int dims = out.dims;
            const int* sizes = out.size.p;
            outBlob = Mat(dims, sizes, CV_MAKETYPE(desiredDepth, out.channels()));
        }
        else
        {
            outBlob = out;
        }

        RNG seededRng;
        RNG* rng = &theRNG();
        if (hasSeed)
        {
            Cv64suf u;
            u.f = seed;
            seededRng = RNG(u.u ? u.u : 1);
            rng = &seededRng;
        }

        if (outBlob.depth() == CV_32F || outBlob.depth() == CV_64F || outBlob.depth() == CV_16F)
        {
            rng->fill(outBlob, RNG::NORMAL, mean, scale);
        }
        else