OSDN Git Service

[Refactor] #3978 rand_range() を調整して同じ値が入ったら乱数アルゴリズムを走らせずに引数の値をそのまま返すよう高速化した
authorHourier <66951241+Hourier@users.noreply.github.com>
Wed, 1 May 2024 15:20:54 +0000 (00:20 +0900)
committerHourier <66951241+Hourier@users.noreply.github.com>
Wed, 1 May 2024 15:21:40 +0000 (00:21 +0900)
src/term/z-rand.cpp
src/term/z-rand.h

index d253e45..98e4f4b 100644 (file)
@@ -69,7 +69,7 @@ void Rand_state_init(void)
 
 int rand_range(int a, int b)
 {
-    if (a > b) {
+    if (a >= b) {
         return a;
     }
     std::uniform_int_distribution<> d(a, b);
index dda75cc..6b87d73 100644 (file)
@@ -42,10 +42,6 @@ T randnum0(U initial_max)
     requires(std::is_integral_v<T> || std::is_enum_v<T>) && (std::is_integral_v<U> || std::is_enum_v<U>)
 {
     const auto max = static_cast<int>(initial_max);
-    if (max == 0) {
-        return static_cast<T>(0);
-    }
-
     return max > 0 ? static_cast<T>(rand_range(0, max - 1)) : -static_cast<T>(rand_range(0, -max - 1));
 }