Commit 507f45a2 authored by Alexander A. Maly's avatar Alexander A. Maly

Trying to fix random number generator for MinGW

It always returned the same value.
parent 551173c9
Pipeline #3452 passed with stages
in 3 minutes and 56 seconds
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include <time.h> #include <time.h>
#if !defined(APPLE) && !defined(USE_MINGW_TOOLCHAIN) #if !defined(APPLE) && !defined(USE_MINGW_TOOLCHAIN)
#include <random> #include <random>
static std::random_device rd;
#endif #endif
namespace Kumir namespace Kumir
...@@ -49,7 +50,6 @@ unsigned int Random::get_sample() ...@@ -49,7 +50,6 @@ unsigned int Random::get_sample()
#if !defined(WIN32) || defined(USE_MINGW_TOOLCHAIN) #if !defined(WIN32) || defined(USE_MINGW_TOOLCHAIN)
return rand(); return rand();
#else #else
std::random_device rd;
return rd() - rd.min(); return rd() - rd.min();
#endif #endif
} }
...@@ -66,6 +66,11 @@ int Random::irand(int a, int b) ...@@ -66,6 +66,11 @@ int Random::irand(int a, int b)
} }
unsigned int d = b - a + 1; unsigned int d = b - a + 1;
if (d == 0) {
Core::abort(Core::fromUtf8("Неверный диапазон чисел"));
return 0;
}
unsigned int rd = get_max(), rq = rd / d + (rd % d + 1) / d; unsigned int rd = get_max(), rq = rd / d + (rd % d + 1) / d;
assert (0 < rq); assert (0 < rq);
...@@ -105,7 +110,7 @@ real Random::rrand(real a, real b) ...@@ -105,7 +110,7 @@ real Random::rrand(real a, real b)
if (v < res) { if (v < res) {
res = v; res = v;
} }
return res; return a + res;
} }
real Random::rrnd(real x) real Random::rrnd(real x)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment