Random.c
Go to the documentation of this file.
1 /*
2  * This file contains a portable random generator. It will give
3  * identical sequences of random integers for any platform with
4  * at least 32-bit integers.
5  *
6  * A version of this generator is described in J. Bentley's column,
7  * "The Software Exploratorium", Unix Review 1991. It is based on
8  * Algorithm A in D. E. Knuth, The Art of Computer Programming,
9  * Vol 2, Section 3.2.2, pp. 172.
10  *
11  * The Random function returns a pseudo-random integer in the range
12  * 0...INT_MAX-1.
13  *
14  * The SRandom function uses the given seed for a new sequence of
15  * pseudo-random numbers.
16  */
17 
18 unsigned Random(void);
19 void SRandom(unsigned Seed);
20 
21 #undef STDLIB_RANDOM
22 /* #define STDLIB_RANDOM */
23 
24 #ifdef STDLIB_RANDOM
25 #include <stdlib.h>
26 unsigned Random()
27 {
28  return rand();
29 }
30 
31 void SRandom(unsigned Seed)
32 {
33  srand(Seed);
34 }
35 
36 #else
37 
38 #include <limits.h>
39 #define PRANDMAX INT_MAX
40 
41 static int a = 0, b = 24, arr[55], initialized = 0;
42 
43 unsigned Random()
44 {
45  int t;
46 
47  if (!initialized)
48  SRandom(7913);
49  if (a-- == 0)
50  a = 54;
51  if (b-- == 0)
52  b = 54;
53  if ((t = arr[a] - arr[b]) < 0)
54  t += PRANDMAX;
55  return (arr[a] = t);
56 }
57 
58 void SRandom(unsigned Seed)
59 {
60  int i, ii, last, next;
61 
62  Seed %= PRANDMAX;
63  arr[0] = last = Seed;
64  for (next = i = 1; i < 55; i++) {
65  ii = (21 * i) % 55;
66  arr[ii] = next;
67  if ((next = last - next) < 0)
68  next += PRANDMAX;
69  last = arr[ii];
70  }
71  initialized = 1;
72  a = 0;
73  b = 24;
74  for (i = 0; i < 165; i++)
75  Random();
76 }
77 
78 #endif
static int arr[55]
Definition: Random.c:41
#define PRANDMAX
Definition: Random.c:39
static int a
Definition: Random.c:41
unsigned Random(void)
Definition: Random.c:43
unsigned Seed
Definition: LKH.h:259
static int b
Definition: Random.c:41
static int initialized
Definition: Random.c:41
void SRandom(unsigned Seed)
Definition: Random.c:58


glkh_solver
Author(s): Francisco Suarez-Ruiz
autogenerated on Mon Jun 10 2019 13:50:26