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

Code before:
        case CV_32F:
            fillRandomNormalImpl<float>(out, mean, scale, has_seed, seed);
            break;
        case CV_64F:
            fillRandomNormalImpl<double>(out, mean, scale, has_seed, seed);
            break;
        case CV_8U:
            fillRandomNormalImpl<uint8_t>(out, mean, scale, has_seed, seed);
            break;
        case CV_8S:
            fillRandomNormalImpl<int8_t>(out, mean, scale, has_seed, seed);
            break;
        case CV_32S:
            fillRandomNormalImpl<int32_t>(out, mean, scale, has_seed, seed);
            break;
        case CV_64S:
            fillRandomNormalImpl<int64_t>(out, mean, scale, has_seed, seed);
            break;
        case CV_16F:
        case CV_16BF:
        {
            Mat tmp(out.size(), CV_MAKETYPE(CV_32F, out.channels()));
            fillRandomNormalImpl<float>(tmp, mean, scale, has_seed, seed);
            tmp.convertTo(out, out.type());
            break;
        }
        default:
            CV_Error_(Error::StsNotImplemented,
                      ("RandomNormalLike: invalid/unsupported tensor type: %s",
                       typeToString(out.type()).c_str()));
        }
    }
}

class RandomNormalLikeLayerImpl CV_FINAL : public RandomNormalLikeLayer
{
public:
    RandomNormalLikeLayerImpl(const LayerParams& params)
    {
        setParamsFrom(params);

        mean = params.get<float>("mean", 0.f);
        scale = params.get<float>("scale", 1.f);
        output_dtype = params.get<int>("output_dtype", -1);
        has_seed = params.has("seed");
        seed = has_seed ? params.get<float>("seed") : 0.f;

        CV_Assert(scale >= 0.f);
    }

    virtual bool supportBackend(int backendId) CV_OVERRIDE

Review comment (refactor):
You can get rid of switch and extra template function just by using cv::RNG::fill: https://docs.opencv.org/4.x/d1/dd6/classcv_1_1RNG.html#ad26f2b09d9868cf108e84c9814aa682d. Just need to extract has_seed/seed logic here. 

Code after:
{
public:
    RandomNormalLikeLayerImpl(const LayerParams& params)
    {
        setParamsFrom(params);

        mean = params.get<float>("mean", 0.f);
        scale = params.get<float>("scale", 1.f);
        int dt = params.get<int>("output_dtype", -1);
        outputType = dt >= 0 ? onnxDataTypeToCV(static_cast<OnnxDataType>(dt)) : -1;
        has_seed = params.has("seed");
        seed = has_seed ? params.get<float>("seed") : 0.f;

        CV_Assert(scale >= 0.f);
    }

    virtual bool supportBackend(int backendId) CV_OVERRIDE
    {
        return backendId == DNN_BACKEND_OPENCV;
    }

    virtual bool dynamicOutputShapes() const CV_OVERRIDE
    {
        return false;
    }

    bool getMemoryShapes(const std::vector<MatShape>& inputs,
                         const int requiredOutputs,
                         std::vector<MatShape>& outputs,
                         std::vector<MatShape>& internals) const CV_OVERRIDE
    {
        CV_Assert(inputs.size() == (size_t)1);
        CV_Assert(requiredOutputs == 1);
        outputs.assign(1, inputs[0]);
        internals.clear();
        return true;
    }

    void getTypes(const std::vector<MatType>& inputs,
                  const int requiredOutputs,
                  const int requiredInternals,
                  std::vector<MatType>& outputs,
                  std::vector<MatType>& internals) const CV_OVERRIDE
    {
        CV_Assert(inputs.size() == (size_t)1);
        int outType = outputType >= 0 ? outputType : inputs[0];
        outputs.assign(1, outType);
        CV_Assert(requiredInternals == 0);
        internals.clear();
    }