Parcourir la source

* High order bit randomness woot

svn: 706
Bryan Drewery il y a 22 ans
Parent
commit
050527412f
11 fichiers modifiés avec 52 ajouts et 54 suppressions
  1. 1 1
      src/cmds.c
  2. 3 3
      src/dcc.c
  3. 1 1
      src/dccutil.c
  4. 6 0
      src/eggdrop.h
  5. 25 30
      src/misc.c
  6. 10 13
      src/mod/ctcp.mod/ctcp.c
  7. 2 2
      src/mod/irc.mod/irc.c
  8. 1 1
      src/mod/server.mod/servmsg.c
  9. 1 1
      src/mod/share.mod/share.c
  10. 1 1
      src/shell.c
  11. 1 1
      src/users.c

+ 1 - 1
src/cmds.c

@@ -1572,7 +1572,7 @@ static void cmd_botcmd(struct userrec *u, int idx, char *par)
         tbots++;
     }
     if (tbots)
-      rleaf = random() % tbots;
+      rleaf = randint(tbots);
   }
   
   for (tbot = tandbot; tbot; tbot = tbot->next) {

+ 3 - 3
src/dcc.c

@@ -77,7 +77,7 @@ static void dcc_telnet_pass(int, int);
 
 char *rand_dccresp()
 {
-  switch (random() % 10) { /* 0-5: random response, 6-9: none */
+  switch (randint(10)) { /* 0-5: random response, 6-9: none */
   case 0:
     return STR("sup\n");
   case 1:
@@ -106,7 +106,7 @@ char *rand_dccresp()
 
 char *rand_dccresppass()
 {
-  switch (random() % 10) { /* 0-5: random response, 6-9: none */
+  switch (randint(10)) { /* 0-5: random response, 6-9: none */
   case 0:
     return STR("what?\n");
   case 1:
@@ -129,7 +129,7 @@ char *rand_dccresppass()
 }
 char *rand_dccrespbye()
 {
-  switch (random() % 10) { /* 0-5: random response, 6-9: none */
+  switch (randint(10)) { /* 0-5: random response, 6-9: none */
   case 0:
     return STR("stop wasting my time.\n");
   case 1:

+ 1 - 1
src/dccutil.c

@@ -461,7 +461,7 @@ void set_away(int idx, char *s)
  */
 void makepass(char *s)
 {
-  make_rand_str(s, 10 + (random() % 6));
+  make_rand_str(s, 10 + randint(6));
 }
 
 void flush_lines(int idx, struct chat_info *ci)

+ 6 - 0
src/eggdrop.h

@@ -92,6 +92,12 @@
 #  define random() (rand()/16)
 #endif
 
+/* Use high-order bits for getting the random integer. With random()
+ * modulo would probably be sufficient but on systems lacking random(),
+ * the function will be just renamed rand().
+ */
+#define randint(n) (unsigned long) (random() / (RAND_MAX + 1.0) * ((n) < 0 ? (-(n)) : (n)))
+
 
 /***********************************************************************/
 

+ 25 - 30
src/misc.c

@@ -431,9 +431,7 @@ void daysdur(time_t now, time_t then, char *out)
 /* show l33t banner */
 
 char *wbanner() {
-  int r = random();
-
-  switch (r % 9) {
+  switch (randint(9)) {
    case 0: return STR("                       .__  __  .__\n__  _  ______________  |__|/  |_|  |__\n\\ \\/ \\/ /\\_  __ \\__  \\ |  \\   __\\  |  \\\n \\     /  |  | \\// __ \\|  ||  | |   Y  \\\n  \\/\\_/   |__|  (____  /__||__| |___|  /\n                     \\/              \\/\n");
    case 1: return STR("                    _ _   _     \n__      ___ __ __ _(_) |_| |__  \n\\ \\ /\\ / / '__/ _` | | __| '_ \\ \n \\ V  V /| | | (_| | | |_| | | |\n  \\_/\\_/ |_|  \\__,_|_|\\__|_| |_|\n");
    case 2: return STR("@@@  @@@  @@@  @@@@@@@    @@@@@@   @@@  @@@@@@@  @@@  @@@\n@@@  @@@  @@@  @@@@@@@@  @@@@@@@@  @@@  @@@@@@@  @@@  @@@\n@@!  @@!  @@!  @@!  @@@  @@!  @@@  @@!    @@!    @@!  @@@\n!@!  !@!  !@!  !@!  @!@  !@!  @!@  !@!    !@!    !@!  @!@\n@!!  !!@  @!@  @!@!!@!   @!@!@!@!  !!@    @!!    @!@!@!@!\n!@!  !!!  !@!  !!@!@!    !!!@!!!!  !!!    !!!    !!!@!!!!\n!!:  !!:  !!:  !!: :!!   !!:  !!!  !!:    !!:    !!:  !!!\n:!:  :!:  :!:  :!:  !:!  :!:  !:!  :!:    :!:    :!:  !:!\n :::: :: :::   ::   :::  ::   :::   ::     ::    ::   :::\n  :: :  : :     :   : :   :   : :  :       :      :   : :\n");
@@ -553,15 +551,15 @@ void make_rand_str(char *s, int len)
   int j, r = 0;
 
   for (j = 0; j < len; j++) {
-    r = random();
-    if (r % 4 == 0)
-      s[j] = '0' + (random() % 10);
-    else if (r % 4 == 1)
-      s[j] = 'a' + (random() % 26);
-    else if (r % 4 == 2)
-      s[j] = 'A' + (random() % 26);
-    else
-      s[j] = '!' + (random() % 15);
+    r = randint(4);
+    if (r == 0)
+      s[j] = '0' + randint(10);
+    else if (r == 1)
+      s[j] = 'a' + randint(26);
+    else if (r == 2)
+      s[j] = 'A' + randint(26);
+    else if (r == 3)
+      s[j] = '!' + randint(15);
 
     if (s[j] == 33 || s[j] == 37 || s[j] == 34 || s[j] == 40 || s[j] == 41 || s[j] == 38 || s[j] == 36) /* no % ( ) & */
       s[j] = 35;
@@ -597,7 +595,7 @@ char *str_escape(const char *str, const char div, const char mask)
   const int	 len = strlen(str);
   int		 buflen = (2 * len), blen = 0;
   char		*buf = NULL, *b = NULL;
-  const char	*s;
+  const char	*s = NULL;
 
   b = buf = malloc(buflen + 1);
 
@@ -873,12 +871,11 @@ int bot_aggressive_to(struct userrec *u)
 
 char kickprefix[25] = "";
 char bankickprefix[25] = "";
-char *kickreason(int kind) {
-  int r = random();
 
+char *kickreason(int kind) {
   switch (kind) {
   case KICK_BANNED:
-    switch (r % 6) {
+    switch (randint(6)) {
     case 0: return STR("bye");
     case 1: return STR("banned");
     case 2: return STR("see you in hell");
@@ -887,21 +884,21 @@ char *kickreason(int kind) {
     case 5: return STR("unwanted!");
     }
   case KICK_KUSER:
-    switch (r % 4) {
+    switch (randint(4)) {
     case 0: return STR("not wanted");
     case 1: return STR("something tells me you're annoying");
     case 2: return STR("don't bug me lewser");
     case 3: return STR("creep");
     }
   case KICK_KICKBAN:
-    switch (r % 4) {
+    switch (randint(4)) {
     case 0: return STR("gone");
     case 1: return STR("stupid");
     case 2: return STR("lewser");
     case 3: return STR("...");
     }     
   case KICK_MASSDEOP:
-    switch (r % 8) {
+    switch (randint(8)) {
     case 0: return STR("spammer!");
     case 1: return STR("easy on the modes now");
     case 2: return STR("mode this");
@@ -912,7 +909,7 @@ char *kickreason(int kind) {
     case 7: return STR("i win kthx");
     }
   case KICK_BADOP:
-    switch (r % 5) {
+    switch (randint(5)) {
     case 0: return STR("neat...");
     case 1: return STR("oh, no you don't. go away.");
     case 2: return STR("didn't you forget something now?");
@@ -920,7 +917,7 @@ char *kickreason(int kind) {
     case 4: return STR("hijack this");
     }
   case KICK_BADOPPED:
-    switch (r % 5) {
+    switch (randint(5)) {
     case 0: return STR("fuck off kid");
     case 1: return STR("asl?");
     case 2: return STR("whoa... what a hacker... skills!");
@@ -928,7 +925,7 @@ char *kickreason(int kind) {
     case 4: return STR("with your skills, you're better off jacking off than hijacking");
     }
   case KICK_MANUALOP:
-    switch (r % 6) {
+    switch (randint(6)) {
     case 0: return STR("naughty kid");
     case 1: return STR("didn't someone tell you that is bad?");
     case 2: return STR("want perm?");
@@ -937,7 +934,7 @@ char *kickreason(int kind) {
     case 5: return STR("jackass!");
     }
   case KICK_MANUALOPPED:
-    switch (r % 8) {
+    switch (randint(8)) {
     case 0: return STR("your pal got mean friends. like me.");
     case 1: return STR("uhh now.. don't wake me up...");
     case 2: return STR("hi hun. missed me?");
@@ -948,7 +945,7 @@ char *kickreason(int kind) {
     case 7: return STR("lol, really?");
     }
   case KICK_CLOSED:
-    switch (r % 17) {
+    switch (randint(17)) {
     case 0: return STR("locked");
     case 1: return STR("later");
     case 2: return STR("closed for now");
@@ -968,7 +965,7 @@ char *kickreason(int kind) {
     case 16: return STR("closed. try tomorrow");
     }
   case KICK_FLOOD:
-    switch (r % 7) {
+    switch (randint(7)) {
     case 0: return STR("so much bullshit in such a short time. amazing.");
     case 1: return STR("slow down. i'm trying to read here.");
     case 2: return STR("uhm... you actually think irc is for talking?");
@@ -979,7 +976,7 @@ char *kickreason(int kind) {
 
     }
   case KICK_NICKFLOOD:
-    switch (r % 7) {
+    switch (randint(7)) {
     case 0: return STR("make up your mind?");
     case 1: return STR("be schizofrenic elsewhere");
     case 2: return STR("I'm loosing track of you... not!");
@@ -989,19 +986,18 @@ char *kickreason(int kind) {
     case 6: return STR("gotcha!");
     }
   case KICK_KICKFLOOD:
-    switch (r % 6) {
+    switch (randint(6)) {
     case 0: return STR("easier to just leave if you wan't to be alone");
     case 1: return STR("cool down");
     case 2: return STR("don't be so damned aggressive. that's my job.");
     case 3: return STR("kicking's fun, isn't it?");
     case 4: return STR("what's the rush?");
     case 5: return STR("next time you do that, i'll kick you again");
-
     }
   case KICK_BOGUSUSERNAME:
     return STR("bogus username");
   case KICK_MEAN:
-    switch (r % 11) {
+    switch (randint(11)) {
     case 0: return STR("hey! that wasn't very nice!");
     case 1: return STR("don't fuck with my pals");
     case 2: return STR("meanie!");
@@ -1019,7 +1015,6 @@ char *kickreason(int kind) {
   default:
     return "OMFG@YUO";    
   }
-
 }
 
 /*

+ 10 - 13
src/mod/ctcp.mod/ctcp.c

@@ -130,7 +130,7 @@ void scriptchanged()
   {
     char mircver[4] = "";
 
-    switch (random() % 7) {
+    switch (randint(7)) {
       case 0:
         strcpy(mircver, "6.01");
         break;
@@ -169,7 +169,7 @@ void scriptchanged()
   {
     char theme[30] = "";
 
-    switch (random() % 25) { /* 0-19 = script, 20-24 = plain */
+    switch (randint(25)) { /* 0-19 = script, 20-24 = plain */
     case 0:
       strcpy(theme, STR(" \037.\037.\002BX\002"));
       break;
@@ -234,7 +234,7 @@ void scriptchanged()
       strcpy(theme, STR(""));
       break;
     }
-    switch (random() % 16) {
+    switch (randint(16)) {
     case 0:
       sprintf(ctcpversion, STR("bitchx\037-\037%s \037/\037 cypress\037.\03701i%s"), cloak_bxver, theme);
       break;
@@ -391,22 +391,19 @@ static void ctcp_minutely()
   int i;
 
 #ifdef S_AUTOAWAY
-  int n;
-
   if ((cloak_awaytime == 0) && (cloak_heretime == 0)) {
     cloak_heretime = time(NULL);
     dprintf(DP_HELP, STR("AWAY :\n"));
     return;
   }
-  n = random();
   if (cloak_awaytime == 0) {
-    if (!(n % AVGHERETIME)) {
+    if (!randint(AVGHERETIME)) {
       cloak_heretime = 0;
-      cloak_awaytime = time(NULL) - 600 - random() % 60;
+      cloak_awaytime = time(NULL) - 600 - randint(60);
       sendaway();
     }
   } else {
-    if (!(n % AVGAWAYTIME)) {
+    if (!randint(AVGAWAYTIME)) {
       cloak_awaytime = 0;
       cloak_heretime = time(NULL);
       dprintf(DP_HELP, STR("AWAY :\n"));
@@ -474,7 +471,7 @@ static int ctcp_VERSION(char *nick, char *uhost, struct userrec *u, char *object
   char s[50] = "";
 
   if (cloak_script == CLOAK_CYPRESS) {
-    switch (random() % 8) {
+    switch (randint(8)) {
     case 0:
       strcpy(s, STR(" :should of put the glock down."));
       break;
@@ -699,7 +696,7 @@ void cloak_changed(struct cfg_entry *cfgent, char *oldval, int *valid)
   i = atoi(p);
 #ifdef LEAF
   if (i == 0)
-    i = (random() % CLOAK_COUNT) + 1;
+    i = randint(CLOAK_COUNT) + 1;
 #endif /* LEAF */
   if ((*valid = ((i >= 0) && (i <= CLOAK_COUNT))))
     cloak_script = i;
@@ -727,7 +724,7 @@ void ctcp_init()
     strncpyz(cloak_host, un.nodename, sizeof(cloak_host));
   } else {
     /* shit, we have to come up with something ourselves.. */
-    switch (random() % 2) {
+    switch (randint(2)) {
     case 0:
       strcpy(cloak_os, STR("Linux"));
       strcpy(cloak_osver, STR("2.4.20"));
@@ -742,7 +739,7 @@ void ctcp_init()
   if ((p = strchr(cloak_host, '.')))
     *p = 0;
 
-  switch (random() % 4) {
+  switch (randint(4)) {
   case 0:
     strcpy(cloak_bxver, STR("1.0c17"));
     break;

+ 2 - 2
src/mod/irc.mod/irc.c

@@ -556,7 +556,7 @@ static void request_op(struct chanset_t *chan)
 
   /* Pick random op and ask for ops */
   while (cnt) {
-    i2 = random() % i;
+    i2 = randint(i);
     if (botops[i2]) {
       putbot(botops[i2]->user->handle, s);
       chan->opreqtime[first] = n;
@@ -607,7 +607,7 @@ static void request_in(struct chanset_t *chan)
                                           , myipstr(6) ? myipstr(6) : ".");
   l = calloc(1, cnt * 30);
   while (cnt) {
-    n = random() % i;
+    n = randint(i);
     if (botops[n]) {
       putbot(botops[n]->handle, s);
       if (l[0]) {

+ 1 - 1
src/mod/server.mod/servmsg.c

@@ -82,7 +82,7 @@ static int gotfake433(char *from)
         botname[0] = tmp;
       } else {
         /* when we run out of 'altchrs', then make altnick_char a random alpha */
-        altnick_char = 'a' + random() % 26;
+        altnick_char = 'a' + randint(26);
       }
     } else {
       /* else, make altnick_char the 'oknick' */

+ 1 - 1
src/mod/share.mod/share.c

@@ -101,7 +101,7 @@ static void add_delay(struct chanset_t *chan, int plsmns, int mode, char *mask)
   d->mask = (char *) calloc(1, strlen(mask) + 1);
 
   strncpy(d->mask, mask, strlen(mask) + 1);
-  d->seconds = (int) (now + (random() % 20));
+  d->seconds = (int) (now + randint(20));
   d->next = start_delay;
   start_delay = d;
 }

+ 1 - 1
src/shell.c

@@ -883,7 +883,7 @@ void crontab_create(int interval) {
       strcpy(buf, "*");
     else {
       int i = 1;
-      int si = random() % interval;
+      int si = randint(interval);
 
       while (i < 60) {
         if (buf[0])

+ 1 - 1
src/users.c

@@ -1273,7 +1273,7 @@ void autolink_cycle(char *start)
     }
   }
   putlog(LOG_DEBUG, "@", STR("Picking random hub from %d hubs"), hlc);
-  hlc = random() % hlc;
+  hlc = randint(hlc);
   putlog(LOG_DEBUG, "@", STR("Picked #%d for hub"), hlc);
   while (hl) {
     if (!hlc) {