summaryrefslogtreecommitdiffstats
path: root/labb8/lib/StanfordCPPLib/private/randompatch.h
blob: c7a4b868bc656cabe8a18d6f8fb179a6b5f98e07 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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