From b2bf61dbf7b1c83d867f672cd5dad503e92c6362 Mon Sep 17 00:00:00 2001 From: Richard Nyberg Date: Wed, 14 Dec 2005 17:31:16 +0000 Subject: [PATCH] Add and use rand_between. --- btpd/btpd.c | 2 +- btpd/download_subr.c | 2 +- misc/subr.c | 7 +++++++ misc/subr.h | 2 ++ 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 #include #include +#include #include #include #include @@ -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