summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/random.cc
diff options
context:
space:
mode:
authorAdam Borowski <kilobyte@angband.pl>2013-10-06 17:00:22 +0200
committerAdam Borowski <kilobyte@angband.pl>2013-10-06 17:00:22 +0200
commit4ee4bd8e3265b59996e467dcd555a7bccad1f9ee (patch)
treea444c9d537ff5f985ecc7a48e010c5bfc656bb12 /crawl-ref/source/random.cc
parent6cfc1f2ff4dbf4ade01828aca2c2d1cd9c3029ea (diff)
downloadcrawl-ref-4ee4bd8e3265b59996e467dcd555a7bccad1f9ee.tar.gz
crawl-ref-4ee4bd8e3265b59996e467dcd555a7bccad1f9ee.zip
Don't wrap low-level RNG calls twice.
random.cc wrapped rng.cc, rng.cc wrapped asg.cc. The middle level did nothing but pass calls as-is.
Diffstat (limited to 'crawl-ref/source/random.cc')
-rw-r--r--crawl-ref/source/random.cc52
1 files changed, 47 insertions, 5 deletions
diff --git a/crawl-ref/source/random.cc b/crawl-ref/source/random.cc
index 669cb7dd0b..e4923ab79e 100644
--- a/crawl-ref/source/random.cc
+++ b/crawl-ref/source/random.cc
@@ -1,7 +1,49 @@
#include "AppHdr.h"
#include <math.h>
+#include "asg.h"
#include "random.h"
+#include "syscalls.h"
+
+#ifdef UNIX
+// for times()
+#include <sys/times.h>
+#endif
+
+// for getpid()
+#include <sys/types.h>
+#ifndef TARGET_COMPILER_VC
+# include <unistd.h>
+#else
+# include <process.h>
+#endif
+
+void seed_rng(uint32_t seed)
+{
+ uint32_t sarg[1] = { seed };
+ seed_asg(sarg, 1);
+}
+
+void seed_rng()
+{
+ /* Use a 160-bit wide seed */
+ uint32_t seed_key[5];
+ read_urandom((char*)(&seed_key), sizeof(seed_key));
+
+#ifdef UNIX
+ struct tms buf;
+ seed_key[0] += times(&buf);
+#endif
+ seed_key[1] += getpid();
+ seed_key[2] += time(NULL);
+
+ seed_asg(seed_key, 5);
+}
+
+uint32_t random_int()
+{
+ return get_uint32();
+}
// [low, high]
int random_range(int low, int high)
@@ -59,7 +101,7 @@ int random2(int max)
while (true)
{
- uint32_t bits = random_int();
+ uint32_t bits = get_uint32();
uint32_t val = bits / partn;
if (val < (uint32_t)max)
@@ -257,13 +299,13 @@ int binomial_generator(unsigned n_trials, unsigned trial_prob)
// range [0, 1.0)
double random_real()
{
- return random_int() / 4294967296.0;
+ return get_uint32() / 4294967296.0;
}
// range [0, 1.0]
double random_real_inc()
{
- return random_int() / 4294967295.0;
+ return get_uint32() / 4294967295.0;
}
// range [0, 1.0], weighted to middle with multiple rolls
@@ -332,7 +374,7 @@ bool defer_rand::x_chance_in_y_contd(int x, int y, int index)
do
{
if (index == int(bits.size()))
- bits.push_back(random_int());
+ bits.push_back(get_uint32());
uint64_t expn_rand_1 = uint64_t(bits[index++]) * y;
uint64_t expn_rand_2 = expn_rand_1 + y;
@@ -355,7 +397,7 @@ int defer_rand::random2(int maxp1)
return 0;
if (bits.empty())
- bits.push_back(random_int());
+ bits.push_back(get_uint32());
uint64_t expn_rand_1 = uint64_t(bits[0]) * maxp1;
uint64_t expn_rand_2 = expn_rand_1 + maxp1;