Browse Source

randint, randrange.

Steve Thielemann 4 years ago
parent
commit
2b91ae9fc6
1 changed files with 17 additions and 2 deletions
  1. 17 2
      mystic.cpp

+ 17 - 2
mystic.cpp

@@ -47,6 +47,16 @@ static void file_output_open(const char *const log_path) {
 
 #include "lastseen.h"
 
+// http://c-faq.com/lib/randrange.html
+int randint(int N) { return rand() / (RAND_MAX / N + 1); }
+
+// http://c-faq.com/lib/randrange.html
+// numbers in the range [M, N] could be generated with something like
+
+int randrange(int M, int N) {
+  return M + rand() / (RAND_MAX / (N - M + 1) + 1);
+}
+
 /*
 What is the name of the actual, real Mystic executable
 that we'll be executing and mangling?
@@ -902,6 +912,8 @@ void render(int fd, const char *string_out) {
 
 // Beanzilla's no repeats
 
+#ifdef NOPE
+
 /**
  * have_seen( list, len, item )
  *
@@ -941,6 +953,8 @@ void init_have_seen(int *list, int len) {
   }
 }
 
+#endif
+
 /*
     These are harry "timeout" events.
 
@@ -984,8 +998,9 @@ void harry_idle_event(int fd) {
   // and don't repeat (the last two)!
 
   do {
-    r = random() % ((sizeof(phrases) / sizeof(char *)) - 1);
-  } while (!last_seen_harry_event.seen_before(r));
+    r = randint((sizeof(phrases) / sizeof(char *)));
+    // r = random() % ((sizeof(phrases) / sizeof(char *)) - 1);
+  } while (last_seen_harry_event.seen_before(r));
 
   // ZF_LOGD("%d => %d %d", r, last_seen_harry_event[0],
   // last_seen_harry_event[1]);