summaryrefslogtreecommitdiffstats
path: root/labb8/lib/StanfordCPPLib/private/randompatch.h
diff options
context:
space:
mode:
authorGustav Sörnäs <gustav@sornas.net>2020-12-03 17:11:43 +0100
committerGustav Sörnäs <gustav@sornas.net>2020-12-08 10:21:07 +0100
commit0c39051ba80f04b1177833a006f2d442a7170b56 (patch)
tree9e657946a061b5b305f9cf75634db7b37e979eb3 /labb8/lib/StanfordCPPLib/private/randompatch.h
parent7b7f6808a7b2db2ed21103767434c1445f7815c2 (diff)
downloadtddd86-0c39051ba80f04b1177833a006f2d442a7170b56.tar.gz
add initial files l8
Diffstat (limited to 'labb8/lib/StanfordCPPLib/private/randompatch.h')
-rwxr-xr-xlabb8/lib/StanfordCPPLib/private/randompatch.h63
1 files changed, 63 insertions, 0 deletions
diff --git a/labb8/lib/StanfordCPPLib/private/randompatch.h b/labb8/lib/StanfordCPPLib/private/randompatch.h
new file mode 100755
index 0000000..c7a4b86
--- /dev/null
+++ b/labb8/lib/StanfordCPPLib/private/randompatch.h
@@ -0,0 +1,63 @@
+/*
+ * File: private/randompatch.h
+ * ---------------------------
+ * This file patches the implementation of the random number library
+ * to avoid some serious bugs in standard implementations of rand,
+ * particularly on Mac OS X. It also includes a hack to set the
+ * seed from the RANDOM_SEED environment variable, which makes it
+ * possible to produce repeatable figures.
+ */
+
+/*
+ * Implementation notes: rand, srand
+ * ---------------------------------
+ * To ensure that this package works the same way on all platforms,
+ * this file completely reimplements the rand and srand methods. The
+ * algorithm is a conventional linear congruential generator with the
+ * parameters used in GNU's gclib. RAND_MAX for this implementation
+ * is 2147483647 (2^31 - 1).
+ */
+
+#define MULTIPLIER 1103515245
+#define OFFSET 12345
+
+static int _seed = 1;
+
+#undef rand
+#define rand() ((_seed = MULTIPLIER * _seed + OFFSET) & 0x7FFFFFFF)
+
+#undef srand
+#define srand(seed) (_seed = int(seed), _seed = (_seed <= 0) ? 1 : _seed)
+
+#undef RAND_MAX
+#define RAND_MAX 2147483647
+
+/*
+ * Implementation notes: Windows patch
+ * -----------------------------------
+ * On some versions of Windows, the time function is too coarse to use
+ * as a random seed. On those versions, this definition substitutes the
+ * GetTickCount function.
+ */
+
+#if defined (_MSC_VER) && (_MSC_VER >= 1200)
+# include <windows.h>
+# define time(dummy) (GetTickCount())
+#endif
+
+#ifdef __APPLE__
+
+# include <cstdlib>
+
+ static time_t patchedTime(time_t *) {
+ char *str = getenv("RANDOM_SEED");
+ if (str == NULL) {
+ return time(NULL);
+ } else {
+ return atoi(str);
+ }
+ }
+
+# define time(dummy) patchedTime(dummy)
+
+#endif