summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/random.cc
diff options
context:
space:
mode:
authorNeil Moore <neil@s-z.org>2014-06-26 21:47:14 -0400
committerNeil Moore <neil@s-z.org>2014-06-26 22:01:01 -0400
commitf5b527b32e7d12f89831272eeeeb58e97f73e2ee (patch)
tree04331320f3925690e6fe7b7047c90afb8c2bd002 /crawl-ref/source/random.cc
parent6df916d868ea5be1dee8d6276e09fa4c7e4cedc8 (diff)
downloadcrawl-ref-f5b527b32e7d12f89831272eeeeb58e97f73e2ee.tar.gz
crawl-ref-f5b527b32e7d12f89831272eeeeb58e97f73e2ee.zip
Deduplicate some code.
Diffstat (limited to 'crawl-ref/source/random.cc')
-rw-r--r--crawl-ref/source/random.cc25
1 files changed, 9 insertions, 16 deletions
diff --git a/crawl-ref/source/random.cc b/crawl-ref/source/random.cc
index 7fb382fc61..d3b0342628 100644
--- a/crawl-ref/source/random.cc
+++ b/crawl-ref/source/random.cc
@@ -115,8 +115,7 @@ const char* random_choose_weighted(int weight, const char* first, ...)
#define UINT32_MAX ((uint32_t)(-1))
#endif
-// [0, max)
-int random2(int max)
+static int _random2(int max, int rng)
{
if (max <= 1)
return 0;
@@ -125,7 +124,7 @@ int random2(int max)
while (true)
{
- uint32_t bits = get_uint32();
+ uint32_t bits = get_uint32(rng);
uint32_t val = bits / partn;
if (val < (uint32_t)max)
@@ -133,22 +132,16 @@ int random2(int max)
}
}
+// [0, max)
+int random2(int max)
+{
+ return _random2(max, 0);
+}
+
// [0, max), separate RNG state
int ui_random(int max)
{
- if (max <= 1)
- return 0;
-
- uint32_t partn = UINT32_MAX / max;
-
- while (true)
- {
- uint32_t bits = get_uint32(1);
- uint32_t val = bits / partn;
-
- if (val < (uint32_t)max)
- return (int)val;
- }
+ return _random2(max, 1);
}
// [0, 1]