This repository has been archived by the owner on Apr 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRandom.c
175 lines (146 loc) · 4.41 KB
/
Random.c
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* RogueMain.c
* Brogue
*
* Created by Brian Walker on 12/26/08.
* Copyright 2012. All rights reserved.
*
* This file is part of Brogue.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <math.h>
#include <time.h>
#include <limits.h>
#include <stdint.h> // C99
#include "Rogue.h"
#include "IncludeGlobals.h"
short randClump(randomRange theRange) {
return randClumpedRange(theRange.lowerBound, theRange.upperBound, theRange.clumpFactor);
}
// Get a random int between lowerBound and upperBound, inclusive, with probability distribution
// affected by clumpFactor.
short randClumpedRange(short lowerBound, short upperBound, short clumpFactor) {
if (upperBound <= lowerBound) {
return lowerBound;
}
if (clumpFactor <= 1) {
return rand_range(lowerBound, upperBound);
}
short i, total = 0, numSides = (upperBound - lowerBound) / clumpFactor;
for(i=0; i < (upperBound - lowerBound) % clumpFactor; i++) {
total += rand_range(0, numSides + 1);
}
for(; i< clumpFactor; i++) {
total += rand_range(0, numSides);
}
return (total + lowerBound);
}
// Get a random int between lowerBound and upperBound, inclusive
boolean rand_percent(short percent) {
return (rand_range(0, 99) < clamp(percent, 0, 100));
}
void shuffleList(short *list, short listLength) {
short i, r, buf;
for (i=0; i<listLength; i++) {
r = rand_range(0, listLength-1);
if (i != r) {
buf = list[r];
list[r] = list[i];
list[i] = buf;
}
}
}
void fillSequentialList(short *list, short listLength) {
short i;
for (i=0; i<listLength; i++) {
list[i] = i;
}
}
//typedef unsigned long int u4;
typedef uint32_t u4;
typedef struct ranctx { u4 a; u4 b; u4 c; u4 d; } ranctx;
static ranctx RNGState[2];
#define rot(x,k) (((x)<<(k))|((x)>>(32-(k))))
u4 ranval( ranctx *x ) {
u4 e = x->a - rot(x->b, 27);
x->a = x->b ^ rot(x->c, 17);
x->b = x->c + x->d;
x->c = x->d + e;
x->d = e + x->a;
return x->d;
}
void raninit( ranctx *x, u4 seed ) {
u4 i;
x->a = 0xf1ea5eed, x->b = x->c = x->d = seed;
for (i=0; i<20; ++i) {
(void)ranval(x);
}
}
/* ----------------------------------------------------------------------
range
returns a number between 0 and N-1
without any bias.
*/
#define RAND_MAX_COMBO ((unsigned long) UINT32_MAX)
int range(int n, short RNG) {
unsigned long div;
int r;
div = RAND_MAX_COMBO/n;
do {
r = ranval(&(RNGState[RNG])) / div;
} while (r >= n);
return r;
}
// Get a random int between lowerBound and upperBound, inclusive, with uniform probability distribution
#ifdef AUDIT_RNG // debug version
int rand_range(int lowerBound, int upperBound) {
int retval;
char RNGMessage[100];
brogueAssert(lowerBound <= INT_MAX && upperBound <= INT_MAX);
if (upperBound <= lowerBound) {
return lowerBound;
}
retval = lowerBound + range(upperBound-lowerBound+1, rogue.RNG);
if (rogue.RNG == RNG_SUBSTANTIVE) {
randomNumbersGenerated++;
if (1) { //randomNumbersGenerated >= 1128397) {
sprintf(RNGMessage, "\n#%lu, %i to %i: %i", randomNumbersGenerated, lowerBound, upperBound, retval);
RNGLog(RNGMessage);
}
}
return retval;
}
#else // normal version
int rand_range(int lowerBound, int upperBound) {
brogueAssert(lowerBound <= INT_MAX && upperBound <= INT_MAX);
if (upperBound <= lowerBound) {
return lowerBound;
}
if (rogue.RNG == RNG_SUBSTANTIVE) {
randomNumbersGenerated++;
}
return lowerBound + range(upperBound-lowerBound+1, rogue.RNG);
}
#endif
// seeds with the time if called with a parameter of 0; returns the seed regardless.
// All RNGs are seeded simultaneously and identically.
unsigned long seedRandomGenerator(unsigned long seed) {
if (seed == 0) {
seed = (unsigned long) time(NULL) - 1352700000;
}
raninit(&(RNGState[RNG_SUBSTANTIVE]), seed);
raninit(&(RNGState[RNG_COSMETIC]), seed);
return seed;
}