Kiwano Engine v1.3.x
Random.h
1// Copyright (c) 2016-2018 Kiwano - Nomango
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19// THE SOFTWARE.
20
21#pragma once
22#include <random>
23
24namespace kiwano
25{
26namespace math
27{
28//
29// 随机数
30//
31// 获取指定范围内的一个随机数, 如:
32// int n = math::Random(1, 5); // 获取 1~5 内的随机整数, 包含 1 和 5
33// 产生的随机数类型取决于参数的类型, 如获取随机浮点数:
34// float d = math::Random(1.2f, 1.5f);
35//
36
37int Random(int min, int max);
38
39unsigned int Random(unsigned int min, unsigned int max);
40
41long Random(long min, long max);
42
43unsigned long Random(unsigned long min, unsigned long max);
44
45char Random(char min, char max);
46
47float Random(float min, float max);
48
49double Random(double min, double max);
50
51//
52// Details of math::Rand
53//
54
55namespace __rand_detail
56{
57inline std::default_random_engine& GetRandomEngine()
58{
59 static std::random_device device;
60 static std::default_random_engine engine(device());
61 return engine;
62}
63
64template <typename T>
65inline T RandomInt(T min, T max)
66{
67 std::uniform_int_distribution<T> dist(min, max);
68 return dist(GetRandomEngine());
69}
70
71template <typename T>
72inline T RandomReal(T min, T max)
73{
74 std::uniform_real_distribution<T> dist(min, max);
75 return dist(GetRandomEngine());
76}
77} // namespace __rand_detail
78
79inline int Random(int min, int max)
80{
81 return __rand_detail::RandomInt(min, max);
82}
83
84inline unsigned int Random(unsigned int min, unsigned int max)
85{
86 return __rand_detail::RandomInt(min, max);
87}
88
89inline long Random(long min, long max)
90{
91 return __rand_detail::RandomInt(min, max);
92}
93
94inline unsigned long Random(unsigned long min, unsigned long max)
95{
96 return __rand_detail::RandomInt(min, max);
97}
98
99inline char Random(char min, char max)
100{
101 return static_cast<char>(__rand_detail::RandomInt(static_cast<int>(min), static_cast<int>(max)));
102}
103
104inline float Random(float min, float max)
105{
106 return __rand_detail::RandomReal(min, max);
107}
108
109inline double Random(double min, double max)
110{
111 return __rand_detail::RandomReal(min, max);
112}
113} // namespace math
114} // namespace kiwano