Quantcast
Channel: MarsHut
Viewing all articles
Browse latest Browse all 6551

better random in atc

$
0
0
We have to support a seed, but if we don't start with one, use
arc4random() instead.

This also makes the linker warnings a little nicer and more localized:
update.o(.text+0xbfd): In function `setseed':
: warning: srandom() seed choices are invariably poor
update.o(.text+0x259): In function `addplane':
: warning: random() isn't random; consider using arc4random()

Index: extern.h
===================================================================
RCS file: /cvs/src/games/atc/extern.h,v
retrieving revision 1.7
diff -u -p -r1.7 extern.h
--- extern.h 25 Oct 2013 21:57:10 -0000 1.7
+++ extern.h 13 Jul 2014 13:21:46 -0000
@@ -102,6 +102,7 @@ void quit(int);
int read_file(const char *);
void redraw(void);
void rezero(void);
+void setseed(const char *);
void setup_screen(const C_SCREEN *);
int too_close(const PLANE *p1, const PLANE *p2, int);
void update(int);
Index: main.c
===================================================================
RCS file: /cvs/src/games/atc/main.c,v
retrieving revision 1.22
diff -u -p -r1.22 main.c
--- main.c 13 Jul 2014 13:00:40 -0000 1.22
+++ main.c 13 Jul 2014 13:21:46 -0000
@@ -111,10 +111,8 @@ main(int ac, char *av[])
ptr++;

- if (seed != NULL)
- srandom(atol(seed));
- else
- srandomdev();
+ if (seed != NULL)
+ setseed(seed);

if (f_usage)
fprintf(stderr,
Index: update.c
===================================================================
RCS file: /cvs/src/games/atc/update.c,v
retrieving revision 1.13
diff -u -p -r1.13 update.c
--- update.c 13 Jul 2014 13:00:40 -0000 1.13
+++ update.c 13 Jul 2014 13:21:46 -0000
@@ -43,6 +43,23 @@

#include "include.h"

+int seeded;
+void
+setseed(const char *seed)
+{
+ seeded = 1;
+ srandom(atol(seed));
+}

+uint32_t
+atcrandom()
+{
+ if (seeded)
+ return random();
+ else
+ return arc4random();
+}

void
update(int dummy)

@@ -196,7 +213,7 @@ update(int dummy)
* Otherwise, prop jobs show up *on* entrance. Remember that
* we don't update props on odd updates.
*/
- if ((random() % sp->newplane_time) == 0)
+ if ((atcrandom() % sp->newplane_time) == 0)
addplane();

@@ -292,10 +309,10 @@ addplane(void)
memset( 0, sizeof (p));

p.status = S_MARKED;
- p.plane_type = random() % 2;
+ p.plane_type = atcrandom() % 2;

num_starts = sp->num_exits + sp->num_airports;
- rnd = random() % num_starts;
+ rnd = atcrandom() % num_starts;

if (rnd < sp->num_exits) {
p.dest_type = T_EXIT;
@@ -308,7 +325,7 @@ addplane(void)
/* loop until we get a plane not near another */
for (i = 0; i < num_starts; i++) {
/* loop till we get a different start point */
- while ((rnd2 = random() % num_starts) == rnd)
+ while ((rnd2 = atcrandom() % num_starts) == rnd)

if (rnd2 < sp->num_exits) {
p.orig_type = T_EXIT;

Viewing all articles
Browse latest Browse all 6551

Latest Images

Trending Articles



Latest Images