summary refs log tree commit diff
diff options
context:
space:
mode:
authorRichard Nyberg <rnyberg@murmeldjur.se>2005-12-14 17:31:16 +0000
committerRichard Nyberg <rnyberg@murmeldjur.se>2005-12-14 17:31:16 +0000
commitb2bf61dbf7b1c83d867f672cd5dad503e92c6362 (patch)
treebbdc58438a41144817b8274d16cb25416927bce3
parentc347106fc1aadcee0a13d54a413e65880ac9df2f (diff)
downloadbtpd-b2bf61dbf7b1c83d867f672cd5dad503e92c6362.tar.gz
btpd-b2bf61dbf7b1c83d867f672cd5dad503e92c6362.zip
Add and use rand_between.
-rw-r--r--btpd/btpd.c2
-rw-r--r--btpd/download_subr.c2
-rw-r--r--misc/subr.c7
-rw-r--r--misc/subr.h2
4 files changed, 11 insertions, 2 deletions
diff --git a/btpd/btpd.c b/btpd/btpd.c
index 683b438..6775595 100644
--- a/btpd/btpd.c
+++ b/btpd/btpd.c
@@ -145,7 +145,7 @@ btpd_init(void)
     m_peer_id[sizeof(BTPD_VERSION) - 1] = '|';
     srandom(time(NULL));
     for (int i = sizeof(BTPD_VERSION); i < 20; i++)
-        m_peer_id[i] = rint(random() * 255.0 / RAND_MAX);
+        m_peer_id[i] = rand_between(0, 255);
 
     net_init();
     ipc_init();
diff --git a/btpd/download_subr.c b/btpd/download_subr.c
index 677ea55..f77d7db 100644
--- a/btpd/download_subr.c
+++ b/btpd/download_subr.c
@@ -305,7 +305,7 @@ dl_choose_rarest(struct peer *p, uint32_t *res)
         }
     }
     if (min_c > 1) {
-        min_c = 1 + rint((double)random() * (min_c - 1) / RAND_MAX);
+        min_c = rand_between(1, min_c);
         for (i = min_i; min_c > 0; i++) {
             if (dl_piece_startable(p, i)
                 && tp->piece_count[i] == tp->piece_count[min_i]) {
diff --git a/misc/subr.c b/misc/subr.c
index f894812..77e3a06 100644
--- a/misc/subr.c
+++ b/misc/subr.c
@@ -5,6 +5,7 @@
 #include <fcntl.h>
 #include <inttypes.h>
 #include <limits.h>
+#include <math.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -139,3 +140,9 @@ round_to_page(size_t size)
         size += psize - rem;
     return size;
 }
+
+long
+rand_between(long min, long max)
+{
+    return min + (long)rint((double)random() * (max - min) / RAND_MAX);
+}
diff --git a/misc/subr.h b/misc/subr.h
index 9467839..62c92c4 100644
--- a/misc/subr.h
+++ b/misc/subr.h
@@ -18,4 +18,6 @@ int canon_path(const char *path, char **res);
 
 size_t round_to_page(size_t size);
 
+long rand_between(long min, long max);
+
 #endif