eis/TestProject/RNG/RandT.h

33 lines
736 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma once
#include <time.h>
#include <cmath>
namespace RandT {
template <typename T>
T RandT(T _min, T _max) {
T temp;
if (_min > _max) {
temp = _max;
_max = _min;
_min = temp;
}
return rand() / (double)RAND_MAX * (_max - _min) + _min;
}
double GuassRand(double mean = 0, double sigma = 0) {
double U1 = rand() * 1.0f / RAND_MAX; // 0~1均匀分布
double U2 = rand() * 1.0f / RAND_MAX; // 0~1均匀分布
double Z =
sqrt(-2 * log(U1)) * cos(2 * M_PI * U2); // 均值为0方差为1的正态分布
double Y;
if (sigma >= 0) {
Y = mean + sqrt(sigma) * Z;
} else {
Y = mean + Z;
}
return Y;
}
bool randomBool() { return 0 + (rand() % (1 - 0 + 1)) == 1; }
} // namespace RandT