Sese Framework  x.y.z
A cross-platform framework
载入中...
搜索中...
未找到
RandomUtil.h
浏览该文件的文档.
1// Copyright 2024 libsese
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
23#pragma once
24
25#include <cmath>
26#include <random>
27
28namespace sese {
31public:
32 static constexpr int REQUIRED_UPPER_LETTER = 1;
33 static constexpr int REQUIRED_LOWER_LETTER = 2;
34 static constexpr int REQUIRED_DIGIT = 4;
35 static constexpr int REQUIRED_SYMBOL = 8;
36
37 template<class RETURN_VALUE>
38 static RETURN_VALUE next();
39
40 template<class RETURN_VALUE>
41 static RETURN_VALUE next(RETURN_VALUE min, RETURN_VALUE max);
42
43 static std::string nextString(size_t length, int required);
44
45 static uint64_t nextUnsignedInt64();
46
47 static std::random_device &getDevInstance() noexcept;
48
49private:
50 static std::string nextString(size_t length, bool upper, bool lower, bool digit, bool symbol);
51
52 static const std::string UPPER_LETTER_LIST;
53 static const std::string LOWER_LETTER_LIST;
54 static const std::string DIGIT_LIST;
55 static const std::string SYMBOL_LIST;
56
57 static std::random_device dev;
58};
59} // namespace sese
60
61template<class RETURN_VALUE>
62inline RETURN_VALUE sese::RandomUtil::next() {
63 return static_cast<RETURN_VALUE>(getDevInstance()());
64}
65
66template<class RETURN_VALUE>
67inline RETURN_VALUE sese::RandomUtil::next(RETURN_VALUE min, RETURN_VALUE max) {
68 const auto TEMP_MIN = std::min<RETURN_VALUE>(min, max);
69 const auto TEMP_MAX = std::max<RETURN_VALUE>(min, max);
70 const auto RANGE = TEMP_MAX - TEMP_MIN;
71 auto TMP = getDevInstance()();
72 return static_cast<RETURN_VALUE>(TMP % RANGE) + min;
73}
74
75template<>
76inline float sese::RandomUtil::next() {
77 auto pos = next<uint32_t>(0, UINT32_MAX);
78 return static_cast<float>(pos) / static_cast<float>(UINT32_MAX);
79}
80
81template<>
82inline double sese::RandomUtil::next() {
83 auto pos = next<uint32_t>(0, UINT32_MAX);
84 return static_cast<double>(pos) / static_cast<double>(UINT32_MAX);
85}
86
87template<>
88inline float sese::RandomUtil::next(float min, float max) {
89 const auto TMP_MIN = std::min<float>(min, max);
90 const auto RANGE = std::abs(max - min);
91 const auto TMP = next<float>();
92 return TMP_MIN + RANGE * TMP;
93}
94
95template<>
96inline double sese::RandomUtil::next(double min, double max) {
97 const auto TMP_MIN = std::min<double>(min, max);
98 const auto RANGE = std::abs(max - min);
99 const auto TMP = next<double>();
100 return TMP_MIN + RANGE * TMP;
101}