179a732c7Sopenharmony_ci/*
279a732c7Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
379a732c7Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
479a732c7Sopenharmony_ci * you may not use this file except in compliance with the License.
579a732c7Sopenharmony_ci * You may obtain a copy of the License at
679a732c7Sopenharmony_ci *
779a732c7Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
879a732c7Sopenharmony_ci *
979a732c7Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1079a732c7Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1179a732c7Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1279a732c7Sopenharmony_ci * See the License for the specific language governing permissions and
1379a732c7Sopenharmony_ci * limitations under the License.
1479a732c7Sopenharmony_ci */
1579a732c7Sopenharmony_ci
1679a732c7Sopenharmony_ci#include "dm_random.h"
1779a732c7Sopenharmony_ci
1879a732c7Sopenharmony_ci#include <random>
1979a732c7Sopenharmony_ci
2079a732c7Sopenharmony_ci#if defined(__LITEOS_M__)
2179a732c7Sopenharmony_ci#include <time.h>
2279a732c7Sopenharmony_ci#endif
2379a732c7Sopenharmony_ci
2479a732c7Sopenharmony_cinamespace OHOS {
2579a732c7Sopenharmony_cinamespace DistributedHardware {
2679a732c7Sopenharmony_ciint32_t GenRandInt(int32_t randMin, int32_t randMax)
2779a732c7Sopenharmony_ci{
2879a732c7Sopenharmony_ci#if defined(__LITEOS_M__)
2979a732c7Sopenharmony_ci    srandom(time(NULL));
3079a732c7Sopenharmony_ci    return (randMin + random() % (randMax - randMin));
3179a732c7Sopenharmony_ci#else
3279a732c7Sopenharmony_ci    std::random_device randDevice;
3379a732c7Sopenharmony_ci    std::mt19937 genRand(randDevice());
3479a732c7Sopenharmony_ci    std::uniform_int_distribution<int> disRand(randMin, randMax);
3579a732c7Sopenharmony_ci    return disRand(genRand);
3679a732c7Sopenharmony_ci#endif
3779a732c7Sopenharmony_ci}
3879a732c7Sopenharmony_ci
3979a732c7Sopenharmony_ciint64_t GenRandLongLong(int64_t randMin, int64_t randMax)
4079a732c7Sopenharmony_ci{
4179a732c7Sopenharmony_ci    std::random_device randDevice;
4279a732c7Sopenharmony_ci    std::mt19937 genRand(randDevice());
4379a732c7Sopenharmony_ci    std::uniform_int_distribution<long long> disRand(randMin, randMax);
4479a732c7Sopenharmony_ci    return disRand(genRand);
4579a732c7Sopenharmony_ci}
4679a732c7Sopenharmony_ci} // namespace DistributedHardware
4779a732c7Sopenharmony_ci} // namespace OHOS
48