Micropolis
random.cpp
Go to the documentation of this file.
1 /* random.cpp
2  *
3  * Micropolis, Unix Version. This game was released for the Unix platform
4  * in or about 1990 and has been modified for inclusion in the One Laptop
5  * Per Child program. Copyright (C) 1989 - 2007 Electronic Arts Inc. If
6  * you need assistance with this program, you may contact:
7  * http://wiki.laptop.org/go/Micropolis or email micropolis@laptop.org.
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or (at
12  * your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * General Public License for more details. You should have received a
18  * copy of the GNU General Public License along with this program. If
19  * not, see <http://www.gnu.org/licenses/>.
20  *
21  * ADDITIONAL TERMS per GNU GPL Section 7
22  *
23  * No trademark or publicity rights are granted. This license does NOT
24  * give you any right, title or interest in the trademark SimCity or any
25  * other Electronic Arts trademark. You may not distribute any
26  * modification of this program using the trademark SimCity or claim any
27  * affliation or association with Electronic Arts Inc. or its employees.
28  *
29  * Any propagation or conveyance of this program must include this
30  * copyright notice and these terms.
31  *
32  * If you convey this program (or any modifications of it) and assume
33  * contractual liability for the program to recipients of it, you agree
34  * to indemnify Electronic Arts for any liability that those contractual
35  * assumptions impose on Electronic Arts.
36  *
37  * You may not misrepresent the origins of this program; modified
38  * versions of the program must be marked as such and not identified as
39  * the original program.
40  *
41  * This disclaimer supplements the one included in the General Public
42  * License. TO THE FULLEST EXTENT PERMISSIBLE UNDER APPLICABLE LAW, THIS
43  * PROGRAM IS PROVIDED TO YOU "AS IS," WITH ALL FAULTS, WITHOUT WARRANTY
44  * OF ANY KIND, AND YOUR USE IS AT YOUR SOLE RISK. THE ENTIRE RISK OF
45  * SATISFACTORY QUALITY AND PERFORMANCE RESIDES WITH YOU. ELECTRONIC ARTS
46  * DISCLAIMS ANY AND ALL EXPRESS, IMPLIED OR STATUTORY WARRANTIES,
47  * INCLUDING IMPLIED WARRANTIES OF MERCHANTABILITY, SATISFACTORY QUALITY,
48  * FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT OF THIRD PARTY
49  * RIGHTS, AND WARRANTIES (IF ANY) ARISING FROM A COURSE OF DEALING,
50  * USAGE, OR TRADE PRACTICE. ELECTRONIC ARTS DOES NOT WARRANT AGAINST
51  * INTERFERENCE WITH YOUR ENJOYMENT OF THE PROGRAM; THAT THE PROGRAM WILL
52  * MEET YOUR REQUIREMENTS; THAT OPERATION OF THE PROGRAM WILL BE
53  * UNINTERRUPTED OR ERROR-FREE, OR THAT THE PROGRAM WILL BE COMPATIBLE
54  * WITH THIRD PARTY SOFTWARE OR THAT ANY ERRORS IN THE PROGRAM WILL BE
55  * CORRECTED. NO ORAL OR WRITTEN ADVICE PROVIDED BY ELECTRONIC ARTS OR
56  * ANY AUTHORIZED REPRESENTATIVE SHALL CREATE A WARRANTY. SOME
57  * JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF OR LIMITATIONS ON IMPLIED
58  * WARRANTIES OR THE LIMITATIONS ON THE APPLICABLE STATUTORY RIGHTS OF A
59  * CONSUMER, SO SOME OR ALL OF THE ABOVE EXCLUSIONS AND LIMITATIONS MAY
60  * NOT APPLY TO YOU.
61  */
62 
85 
86 
87 #include "micropolis.h"
88 
89 
91 
92 
99 {
100  nextRandom = nextRandom * 1103515245 + 12345;
101  return (nextRandom & 0xffff00) >> 8;
102 }
103 
104 
110 short Micropolis::getRandom(short range)
111 {
112  int maxMultiple, rnum;
113 
114  range++;
115  maxMultiple = 0xffff / range;
116  maxMultiple *= range;
117 
118  do {
119  rnum = getRandom16();
120  } while (rnum >= maxMultiple);
121 
122  return (rnum % range);
123 }
124 
125 
131 {
132  return simRandom() & 0x0000ffff;
133 }
134 
135 
138 {
139  int i = getRandom16();
140 
141  if (i > 0x7fff) {
142  i = 0x7fff - i;
143  }
144 
145  return i;
146 }
147 
148 
155 short Micropolis::getERandom(short limit)
156 {
157  short z = getRandom(limit);
158  short x = getRandom(limit);
159 
160  return min(z, x);
161 }
162 
163 
166 {
167  struct timeval time;
168  gettimeofday(&time, NULL);
169  seedRandom(time.tv_usec ^ time.tv_sec);
170 }
171 
172 
178 {
179  nextRandom = seed;
180 }
181 
182 
void seedRandom(int seed)
Definition: random.cpp:177
short getRandom(short range)
Definition: random.cpp:110
int getRandom16()
Definition: random.cpp:130
void randomlySeedRandom()
Definition: random.cpp:165
int simRandom()
Definition: random.cpp:98
int getRandom16Signed()
Definition: random.cpp:137
short getERandom(short limit)
Definition: random.cpp:155
Header file for Micropolis game engine.
static T min(const T a, const T b)
Definition: micropolis.h:783